【发布时间】:2018-07-26 12:23:36
【问题描述】:
我收到这个奇怪的错误消息:KeyError: ('var1', 'occured at index 16') 在以下代码行:
df['var1'] = df.apply(lambda row: (row['var1']*row['var2']), axis = 1)
我将 df DataFrame 的 2 列相乘。而16 in df.index 是True。我可以正常访问第 16 行。如果我删除该行,错误仍然存在。
对此有什么想法吗?
编辑: 根据要求,数据样本:
X var1 Y \
0 US4642867729 22.3052 Korea; Republic (S. Korea)
1 US4642867729 5.9139 Korea; Republic (S. Korea)
2 US4642867729 3.0799 Korea; Republic (S. Korea)
3 US4642867729 2.9647 Korea; Republic (S. Korea)
4 US4642867729 2.5798 Korea; Republic (S. Korea)
5 US4642867729 2.5281 Korea; Republic (S. Korea)
6 US4642867729 2.3359 Korea; Republic (S. Korea)
7 US4642867729 2.2434 Korea; Republic (S. Korea)
8 US4642867729 1.8624 Korea; Republic (S. Korea)
W Z \
0 Information Technology US4642867729
1 Information Technology US4642867729
2 Materials US4642867729
3 Health Care US4642867729
4 Information Technology US4642867729
5 Financials US4642867729
6 Consumer Discretionary US4642867729
7 Financials US4642867729
8 Materials US4642867729
var2
0 0.16258420849834973043179786600376246497035026...
1 0.16258420849834973043179786600376246497035026...
2 0.16258420849834973043179786600376246497035026...
3 0.16258420849834973043179786600376246497035026...
4 0.16258420849834973043179786600376246497035026...
5 0.16258420849834973043179786600376246497035026...
6 0.16258420849834973043179786600376246497035026...
7 0.16258420849834973043179786600376246497035026...
8 0.16258420849834973043179786600376246497035026...
感谢您的宝贵时间!
【问题讨论】:
-
您可以发布您的数据样本吗?
-
当然!看看编辑
标签: python-3.x apply