我具有以下多索引jpy_bal
数据帧:
2019-07-28 00:00:00 2019-07-29 00:00:00 (2019-07-28 00:00:00,) (2019-07-29 00:00:00,)
app_vendor_id currency
3 1WO 262 262 2.418967e+00 2.517868e+00
ABX 10 10 9.226124e-01 1.037273e+00
... ... ... ... ...
29193 BCH 8 8 3.372592e+04 3.352354e+04
[129 rows x 4 columns]
然后我尝试将以下代码相乘:
jpy_bal.iloc[:,[0]] = jpy_bal.iloc[:,[0]] * jpy_bal.iloc[:,[3]]
但这给了我以下输出:
2019-07-28 00:00:00 2019-07-29 00:00:00 (2019-07-28 00:00:00,) (2019-07-29 00:00:00,)
app_vendor_id currency
3 1WO Nan 262 2.418967e+00 2.517868e+00
ABX Nan 10 9.226124e-01 1.037273e+00
... ... ... ... ...
29193 BCH Nan 8 3.372592e+04 3.352354e+04
[129 rows x 4 columns]
我有Nan
而不是结果。你知道为什么会这样吗?任何贡献表示赞赏。
答案 0 :(得分:0)
您没有将两个序列相乘,而是将两个数据帧相乘,因为调用了iloc[:, [0]]
而不是.iloc[:, 0]
。这将使用列索引以及行索引,但找不到匹配项。
尝试
jpy_bal.iloc[:, 0] * jpy_bal.iloc[:, 3]
示例
df = pd.DataFrame([[1,2,3],[4,5,6]])
# 2 dataframes with same index, works as expected.
df.iloc[:, [0]] * df.iloc[:, [0]]
# 2 dataframes with different index, yields a 2x2 matrix with NaN's
df.iloc[:, [0]] * df.iloc[:, [1]]
结果
0 1
0 NaN NaN
1 NaN NaN
更正的代码
df.iloc[:, 0] * df.iloc[:, 1]