根据列中的另一个列值有条件地划分列

2024-04-27 00:36:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个数据帧:

Fruit Brand  Supermarket    Sold Revenue    
Apple   A       X            9     2
Apple   A       Y            2     0.5
Apple   B       X            2     1
Orange  A       X            4     1.3
Orange  B       Y            0     0
Banana  A       X            3     0
Apple   B       Z            0     0
.......

我想做的是总结这些列相对的水果类型和品牌。i、 e.a牌苹果排成一行,B牌苹果排成一行,以此类推。你知道吗

但是有一个转折点,我还希望“销售”和“收入”列被划分为每个超市的三个列(X,Y,Z),例如:

Fruit Brand  Supermarket    Sold_x Sold_y Sold_z Revenue_x Revenue_y Revenue_z  
 Apple  A       X            9     0        0     2          0          0
 Apple  A       Y            0     2        0     0          0.5        0

在大熊猫身上最有效的方法是什么?你知道吗


Tags: 数据苹果类型applebanana品牌fruit大熊猫
3条回答

这个解决方案怎么样? 基本上我做了两个groupby,第一个是水果和品牌的groupby,第二个是超市的groupby。你知道吗

cols = ['Fruit', 'Brand', 'Supermarket', 'Sold', 'Revenue']    
df = pd.DataFrame([['Apple',   'A',       'X',            9,     2],
['Apple',   'A',       'Y',            2,     0.5],
['Apple',   'B',       'X',            2,     1],
['Orange',  'A',       'X',            4,     1.3],
['Orange', 'B',       'Y',            0,     0],
['Banana',  'A',       'X',            3,     0],
['Apple',   'B',       'Z',            0,    0]], columns = cols)


out = {}
for key, group in df.groupby(['Fruit', 'Brand']):
    out[key] = {}

    for subkey, subgroup in group.groupby('Supermarket'):

        out[key][subkey] = subgroup[['Supermarket', 'Sold', 'Revenue']].add_suffix("_"+subkey.lower())

    out[key] = pd.concat(out[key], axis = 1)

out = pd.concat(out, axis = 0)
out.columns = out.columns.droplevel(0)

out = out.reset_index(level = 2, drop = True)\
         .fillna(0)\
         .reset_index().rename(columns = {'level_0' : 'Fruit', 'level_1' : 'Brand'})\
         .loc[:, ['Fruit', 'Brand', 'Sold_x', 'Sold_y', 'Sold_z', 'Revenue_x', 'Revenue_y', 'Revenue_z']]

最终结果如下:

out
Out[60]: 
    Fruit Brand  Sold_x  Sold_y  Sold_z  Revenue_x  Revenue_y  Revenue_z
0   Apple     A     9.0     0.0     0.0        2.0        0.0        0.0
1   Apple     A     0.0     2.0     0.0        0.0        0.5        0.0
2   Apple     B     2.0     0.0     0.0        1.0        0.0        0.0
3   Apple     B     0.0     0.0     0.0        0.0        0.0        0.0
4  Banana     A     3.0     0.0     0.0        0.0        0.0        0.0
5  Orange     A     4.0     0.0     0.0        1.3        0.0        0.0
6  Orange     B     0.0     0.0     0.0        0.0        0.0        0.0

这看起来是pivot_table的完美工作:

df.pivot_table(index=['Fruit', 'Brand'], columns='Supermarket').fillna(0)

Out[8]: 
             Sold           Revenue          
Supermarket     X    Y    Z       X    Y    Z
Fruit  Brand                                 
Apple  A      9.0  2.0  0.0     2.0  0.5  0.0
       B      2.0  0.0  0.0     1.0  0.0  0.0
Banana A      3.0  0.0  0.0     0.0  0.0  0.0
Orange A      4.0  0.0  0.0     1.3  0.0  0.0
       B      0.0  0.0  0.0     0.0  0.0  0.0

现在只需重命名列。你知道吗

df.set_index(['Fruit', 'Brand', 'Supermarket']).unstack(fill_value=0.)

enter image description here

相关问题 更多 >