簡體   English   中英

Pandas:如何按一列分組並顯示每組所有其他列的唯一值計數?

[英]Pandas: How to group by one column and show count for unique values for all other columns per group?

問題末尾的數據樣本和嘗試

使用這樣的數據框:

    Type    Class   Area    Decision
0   A       1       North   Yes
1   B       1       North   Yes
2   C       2       South   No
3   A       3       South   No
4   B       3       South   No
5   C       1       South   No
6   A       2       North   Yes
7   B       3       South   Yes
8   B       1       North   No

如何按Decision分組並獲取其他列下唯一值的Decision計數,以便我最終得到:

Decision  Area_North  Aread_South  Class_1  Class_2  Type_A  Type_B  Type_C
Yes       3           1            2        0        2       2       1
No        1           4            1        1        1       2       2

我確信我可以像這樣使用groupby().agg()有一個好的開始:

dfg = df.groupby('Decision').agg({'Type':'count',
                           'Class':'count',
                           'Decision':'count'})

然后旋轉結果,但到目前為止還不夠。 我需要以某種方式包含所有其他列的唯一值。 我確信我在某些地方看到過,您可以將'Position':'count'替換為'Position':pd.Series.unique ,但我似乎無法讓它發揮作用。

代碼:

import pandas as pd

df = pd.DataFrame({'Type': {0: 'A',
                          1: 'B',
                          2: 'C',
                          3: 'A',
                          4: 'B',
                          5: 'C',
                          6: 'A',
                          7: 'B',
                          8: 'B'},
                     'Class': {0: 1, 1: 1, 2: 2, 3: 3, 4: 3, 5: 1, 6: 2, 7: 3, 8: 1},
                     'Area': {0: 'North',
                          1: 'North',
                          2: 'South',
                          3: 'South',
                          4: 'South',
                          5: 'South',
                          6: 'North',
                          7: 'South',
                          8: 'North'},
                     'Decision': {0: 'Yes',
                          1: 'Yes',
                          2: 'No',
                          3: 'No',
                          4: 'No',
                          5: 'No',
                          6: 'Yes',
                          7: 'Yes',
                          8: 'No'}})

dfg = df.groupby('Decision').agg({'Type':'count',
                           'Class':'count',
                           'Decision':'count'})
dfg

DataFrame.meltDataFrame.pivot_table DataFrame.melt使用並展平MultiIndex

df = df.melt('Decision').pivot_table(index='Decision', 
                                     columns=['variable','value'], 
                                     aggfunc='size', 
                                     fill_value=0)
df.columns = df.columns.map('{0[0]}_{0[1]}'.format)
df = df.reset_index()
print (df)
  Decision  Area_North  Area_South  Class_1  Class_2  Class_3  Type_A  Type_B  \
0       No           1           4        2        1        2       1       2   
1      Yes           3           1        2        1        1       2       2   

   Type_C  
0       2  
1       0  

groupby + value_counts melt

s=df.melt('Decision').groupby(['Decision','variable']).\
    value.value_counts().unstack(level=[1,2],fill_value=0)
variable  Area       Class       Type      
value    South North     1  3  2    B  C  A
Decision                                   
No           4     1     2  2  1    2  2  1
Yes          1     3     2  1  1    2  0  2

您還可以通過以下方式修改上述列

s.columns = s.columns.map('{0[0]}_{0[1]}'.format) 

暫無
暫無

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

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