Python日常笔记

求array中出现次数最多的元素

np.argmax(np.bincount(c))

求array中的nan值

np.isnan((c)).any()

pandas中转换行列的类型

  1. to_numeric() - provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See also to_datetime() and to_timedelta().)
  2. astype() - convert (almost) any type to (almost) any other type (even if it’s not necessarily sensible to do so). Also allows you to convert to categorial types (very useful).
  3. infer_objects() - a utility method to convert object columns holding Python objects to a pandas type if possible.
  4. convert_dtypes() - convert DataFrame columns to the “best possible” dtype that supports pd.NA (pandas’ object to indicate a missing value).

获取DataFrame中的某个元素的值

You can use boolean indexing with DataFrame.loc for filter by condition and by column name:

s = df.loc[df['instrument_token'].eq(12295682), 'tradingsymbol']
# alternative
s = df.loc[df['instrument_token'] == 12295682, 'tradingsymbol']

And then get first value of Series:

a = s.iat[0]
a = s.iloc[0]
a = s.tolist()[0]
a = s.to_array()[0]
# general solution if not match condition and select first value failed
a = next(iter(s), 'no match')

Another idea is use DataFrame.set_index fo index by column instrument_token:

df = df.set_index('instrument_token')

Anf then select by DataFrame.loc or DataFrame.at:

a = df.loc[12295682, 'tradingsymbol']
a = df.at[12295682, 'tradingsymbol']

Python打印时间

# 格式化成2016-03-20 11:45:39形式
print (time. strftime(" %Y-%m-%d %H:%M:%S' ,time. localtime()))
# 格式化成Sat Mar 28 22:24:24 2016形式
print (time. strftime("%a %b %d %H:%M:%S %Y",time. localtime()))