Python 2.7 从pd mean()和std()函数获取索引值
我试图从pd标准中获取索引值。 我的最终目标是将索引与另一个df匹配,并插入相应的标准偏差值Python 2.7 从pd mean()和std()函数获取索引值,python-2.7,pandas,Python 2.7,Pandas,我试图从pd标准中获取索引值。 我的最终目标是将索引与另一个df匹配,并插入相应的标准偏差值 (in): df_std['index'] = df_std.index (out): Index([u'AAPL US Equity', u'QQQ US Equity', u'BRABCBACNPR4 BZ Equity'...dtype='object') 但是,由于类型不同,我无法将索引添加到df_std的右侧:df_std.index是一个系列,而df_std是一个df。当我尝试这样做时,
(in): df_std['index'] = df_std.index
(out): Index([u'AAPL US Equity', u'QQQ US Equity', u'BRABCBACNPR4 BZ Equity'...dtype='object')
但是,由于类型不同,我无法将索引添加到df_std的右侧:df_std.index是一个系列,而df_std是一个df。当我尝试这样做时,会添加一行而不是一列:
(in): df_std['index'] = df_std.index
(out):
BRSTNCLF1R25 Govt 64.0864
BRITUBACNPR1 BZ Equity 2.67762
BRSTNCNTB4O9 Govt 48.2419
BRSTNCLF1R74 Govt 64.901
PBR US Equity 0.770755
BRBBASACNOR3 BZ Equity 2.93335
BRSTNCLF1R82 Govt 65.0979
index Index([u'AAPL US Equity', u'QQQ US Equity', u'...
dtype: object
我已经尝试将它转换为一个元组和一个数据帧
谢谢
编辑:
我正在尝试将df_标准['index']与df_最终版['bloomberg_ticker']匹配,并将标准值带到df_最终版['std']:
(in): print df_final
(out):
serie tipo tp_cnpjfundo valor id bloomberg_ticker \
0 NaN caixa NaN NaN 0 NaN
1 NaN titpublicos NaN NaN 1 BRSTNCLF1R17 Govt
2 NaN titpublicos NaN NaN 2 BRSTNCLF1R17 Govt
3 NaN titpublicos NaN NaN 3 BRSTNCLF1R25 Govt
列“id”将在以后删除如果您拥有的是数据帧,则使用重置索引,而不是赋值
df_std = df_std.reset_index()
例如:
df = pd.DataFrame([0,1,2,3], index=['a','b','c','d'])
df = df.reset_index()
输出:
index 0
0 a 0
1 b 1
2 c 2
3 d 3
col vales new
0 a 5 1.0
1 b 1 2.0
2 c 2 3.0
3 d 4 NaN
4 e 5 NaN
我认为我们要做的是将序列的值映射到一个特定的列,这样您就可以使用
df = pd.DataFrame({'col':['a','b','c','d','e'],'vales':[5,1,2,4,5]})
s = pd.Series([1,2,3],index=['a','b','c'])
df['new'] = df['col'].map(s)
输出:
index 0
0 a 0
1 b 1
2 c 2
3 d 3
col vales new
0 a 5 1.0
1 b 1 2.0
2 c 2 3.0
3 d 4 NaN
4 e 5 NaN
只需分配值,序列匹配在分配序列时默认完成。我认为他有一个序列,可能会添加到_frameThank@Wen,可能他拥有的是一个序列。我必须向自己道歉:我正在尝试将df_std的索引与另一个数据帧的特定列匹配,而不是直接插入它们。此代码应为smt。比如:df_std[df_final[index]。containsdf_std[index],na=False][0]对吗?那么您想要映射什么df1['col']。mapdf_std@Bharath非常感谢你!我在问题中加入了df_期末考试。不过,你帮助了我的主要斗争。