簡體   English   中英

pandas hwo to groupby通過計算現有列的值來創建其他列

[英]pandas hwo to groupby create other columns by counting values of existing columns

我在R中知道了如何做到這一點( 如何通過計算現有列來創建新列 ),但我也想知道它在python中是如何工作的。

當原始表格如下所示

 userID   cat1    cat2
    a        f       3
    a        f       3
    a        u       1
    a        m       1
    b        u       2
    b        m       1
    b        m       2

我通過userID對它們進行分組,並希望它變得像

userID   cat1_f  cat1_m  cat1_u  cat2_1  cat2_2  cat2_3
a        2       1       1       2       0       1
b        0       2       1       1       2       0

利用meltGroupBy.sizeunstack

df = (df.melt('userID')
        .groupby(['userID','variable','value'])
        .size()
        .unstack([1,2], fill_value=0))
#python 3.6+
df.columns = [f'{a}_{b}' for a, b in df.columns]
#python bellow
#df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
df = df.reset_index()
print (df)
RangeIndex(start=0, stop=7, step=1)
  userID  cat1_f  cat1_m  cat1_u  cat2_1  cat2_3  cat2_2
0      a       2       1       1       2       2       0
1      b       0       2       1       1       0       2

crosstab替代方案:

df = df.melt('userID')
df = pd.crosstab(df['userID'], [df['variable'], df['value']])
df.columns = [f'{a}_{b}' for a, b in df.columns]
df = df.reset_index()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM