【发布时间】:2016-08-13 15:43:24
【问题描述】:
我有一个 DataFrame (df1)
index abc bcd def
20150101 0.5 0.3 0.2
20150102 0.7 0.9 1.6
20150103 1.7 2.9 4.6
.................
第二个数据帧 (df2)
index a b c ...(about 100 columns)
0 0 1 8 ...
1 9 5 3 ...
2 2 3 7 ..
我想遍历第二个数据帧中的每一列,并且需要在每个循环中形成一个数据帧,例如
index abc bcd def col
20150101 0.5 0.3 0.2 0
20150102 0.7 0.9 1.6 9
20150103 1.7 2.9 4.6 2
并且需要处理这个新的数据框以进行其他计算
我正在运行这个,
for col in df2.iteritems():
df1['new_col'] = col
出现错误:ValueError:值的长度与索引的长度不匹配
如果我像在
中那样从 col 形成一个系列 for col in df2.iteritems():
c = col[1].astype(float)
s = pd.Series(c)
dfb['col'] = s
给予
index abc bcd def col
20150101 0.5 0.3 0.2 NaN
20150102 0.7 0.9 1.6 NaN
20150103 1.7 2.9 4.6 NaN
请提出解决方案。提前致谢!
【问题讨论】:
标签: python loops pandas dataframe multiple-columns