gpt4 book ai didi

python - Pandas 多索引中带有大写字母标签的错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:49:21 24 4
gpt4 key购买 nike

我在下面的代码中遇到了一个错误。如果我的类别列表的第二个元素是大写字母,那么我的数据框“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

最佳答案

你能试试这个吗?通过使用 .loc 选择多索引

for a in categories:
for d in dates:

if a == 'W':
None
else:
change_1_month_df.loc[(a, 'f'), d]=np.random.randn(1)[0]
change_1_month_df.loc[(a, 'g'), d]=np.random.randn(1)[0]

change_1_month_df

关于python - Pandas 多索引中带有大写字母标签的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com