【发布时间】:2018-04-13 21:26:47
【问题描述】:
我在下面的代码中遇到了一个错误。如果我的类别列表的第二个元素是大写字母,那么我的数据框“change_1_month_df”的结果值为 NaN。如果我使用小写字母,则正确插入随机数。有任何想法吗?谢谢
import pandas as pd
import numpy as np
dates = ['d1','d2']
categories = ['a','b']
sub_categories = ['f','g']
my_index = pd.MultiIndex.from_product([categories,sub_categories])
change_1_month_df = pd.DataFrame(index=my_index,columns=dates)
for a in categories:
for d in dates:
print a,d
if a == 'W':
None
else:
change_1_month_df.ix[a].ix['f'][d] = np.random.randn(1)
change_1_month_df.ix[a].ix['g'][d] = np.random.randn(1)
change_1_month_df
【问题讨论】:
-
.ix不推荐使用iloc。 -
谢谢,.loc 问题依然存在
-
是的,我使用的是 2.7