import tushare as ts
import pandas as pd
import numpy as np
data = ts.get_realtime_quotes(['600519','002415','002304','002027','200596'])[['name','price']]
aimprice = ['654','23.5','127.78','1','1']
data['aimprice'] = aimprice
data[['price','aimprice']] = data[['price','aimprice']].apply(pd.to_numeric)
# data['col2'] = data['price'].map(lambda x: x*2)
data['ratio'] = data.apply(lambda x: x['aimprice'] * 2 / x['price'], axis=1)
# data['ratio'] = data['aimprice'] * 2 / data['price']
data['per'] = data.apply(lambda x: 1 / x['ratio'], axis=1)
data['ratio'] = data['ratio'].apply(lambda x: format(x, '.2f'))
data['per'] = data['per'].apply(lambda x: format(x, '.2%'))
# data[['aimprice','ratio','per']].loc[2:] = np.nan
# data.loc[3,'per'] = np.nan
# data.loc[3,'ratio'] = np.nan
# data.loc[4,'per'] = np.nan
# data.loc[4,'ratio'] = np.nan
data.loc[[3,4],['aimprice','ratio','per']] = np.nan
print(data)
name price aimprice ratio per
0 贵州茅台 976.06 654.00 1.34 74.62%
1 海康威视 26.67 23.50 1.76 56.74%
2 洋河股份 129.35 127.78 1.98 50.61%
3 分众传媒 5.24 NaN NaN NaN
4 古井贡B 69.94 NaN NaN NaN
网友评论