gpt4 book ai didi

python - 将新索引添加到 MultiIndex 数据框 pandas 的特定级别

转载 作者:太空狗 更新时间:2023-10-30 02:15:09 24 4
gpt4 key购买 nike

这是我正在尝试做的一个例子:

import io
import pandas as pd
data = io.StringIO('''Fruit,Color,Count,Price
Apple,Red,3,$1.29
Apple,Green,9,$0.99
Pear,Red,25,$2.59
Pear,Green,26,$2.79
Lime,Green,99,$0.39
''')
df_unindexed = pd.read_csv(data)
df = df_unindexed.set_index(['Fruit', 'Color'])

输出:

Out[5]: 
Count Price
Fruit Color
Apple Red 3 $1.29
Green 9 $0.99
Pear Red 25 $2.59
Green 26 $2.79
Lime Green 99 $0.39

现在假设我想计算“颜色”级别中的键数:

L = []
for i in pd.unique(df.index.get_level_values(0)):
L.append(range(df.xs(i).shape[0]))

list(np.concatenate(L))

然后我将结果列表 [0,1,0,1,0] 添加为新列:

df['Bob'] = list(np.concatenate(L))

这样:

             Count  Price  Bob
Fruit Color
Apple Red 3 $1.29 0
Green 9 $0.99 1
Pear Red 25 $2.59 0
Green 26 $2.79 1
Lime Green 99 $0.39 0

我的问题:

如何使 Bob 列成为与 Color 处于同一级别的索引?这就是我想要的:

                 Count  Price
Fruit Color Bob
Apple Red 0 3 $1.29
Green 1 9 $0.99
Pear Red 0 25 $2.59
Green 1 26 $2.79
Lime Green 0 99 $0.39

最佳答案

您是否在寻找cumcount?如果是这样,您可以放弃循环并矢量化您的解决方案。

df = df.set_index(df.groupby(level=0).cumcount(), append=True)
print(df)
Count Price
Fruit Color
Apple Red 0 3 $1.29
Green 1 9 $0.99
Pear Red 0 25 $2.59
Green 1 26 $2.79
Lime Green 0 99 $0.39

或者,如果您希望一次性完成此操作,

df_unindexed = pd.read_csv(data)
df = df_unindexed.set_index(['Fruit', 'Color', df.groupby('Fruit').cumcount()])
print(df)
Count Price
Fruit Color
Apple Green 0 9 $0.99
Red 1 3 $1.29
Lime Green 0 99 $0.39
Pear Green 1 26 $2.79
Red 0 25 $2.59

要重命名索引,请使用 rename_axis:

df = df.rename_axis(['Fruit', 'Color', 'Bob'])
print(df)
Count Price
Fruit Color Bob
Apple Red 0 3 $1.29
Green 1 9 $0.99
Pear Red 0 25 $2.59
Green 1 26 $2.79
Lime Green 0 99 $0.39

关于python - 将新索引添加到 MultiIndex 数据框 pandas 的特定级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488252/

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