大熊猫组按条件频率为每个用户明智

问题描述 投票:2回答:2

我有这样的数据帧

df = pd.DataFrame({
    'User':['101','101','102','102','102'],
    'Product':['x','x','x','z','z'],
    'Country':['India,Brazil','India','India,Brazil,Japan','India,Brazil','Brazil']
})

我希望用户明智地获得国家和产品组合数量,如下所示

首先拆分国家然后与产品结合并计算。

想要输出:

enter image description here

python pandas
2个回答
3
投票

这是在SO上结合其他答案的一种方式(它只显示了搜索的力量:D)

import pandas as pd

df = pd.DataFrame({
    'User':['101','101','102','102','102'],
    'Product':['x','x','x','z','z'],
    'Country':['India,Brazil','India','India,Brazil,Japan','India,Brazil','Brazil']
})

# Making use of: https://stackoverflow.com/a/37592047/7386332
j = (df.Country.str.split(',', expand=True).stack()
                                           .reset_index(drop=True, level=1)
                                           .rename('Country'))
df = df.drop('Country', axis=1).join(j)

# Reformat to get desired Country_Product
df = (df.drop(['Country','Product'], 1)
      .assign(Country_Product=['_'.join(i) for i in zip(df['Country'], df['Product'])]))

df2 = df.groupby(['User','Country_Product'])['User'].count().rename('Count').reset_index()

print(df2)

返回:

  User Country_Product  count
0  101        Brazil_x      1
1  101         India_x      2
2  102        Brazil_x      1
3  102        Brazil_z      2
4  102         India_x      1
5  102         India_z      1
6  102         Japan_x      1

3
投票

怎么样get_dummies

df.set_index(['User','Product']).Country.str.get_dummies(sep=',').replace(0,np.nan).stack().sum(level=[0,1,2])
Out[658]: 
User  Product        
101   x        Brazil    1.0
               India     2.0
102   x        Brazil    1.0
               India     1.0
               Japan     1.0
      z        Brazil    2.0
               India     1.0
dtype: float64
© www.soinside.com 2019 - 2024. All rights reserved.