[英]Find count of unique column elements after using groupby with pandas
[英]Groupby multiple column to find the unique count of one column using python pandas
我有 dataframe 像:
column1 column2 column3
ram tall good
rohan short fine
ajay tall best
alia tall good
aman medium fine
john short good
jack short fine
現在我需要 output 像:
基於 column1-> 的高、短、中的唯一計數
tall=2 , short=1 , medium=0
基於 column1-> 的高、短、中的唯一罰款計數
tall=0 , short=2 , medium=1
基於 column1-> 的高、短、中最佳的唯一計數
tall=1 , short=0 , medium=0
我是 pandas 的初學者。 提前致謝
讓我們試試pd.crosstab
:
pd.crosstab(df['column3'], df['column2'])
column2 medium short tall
column3
best 0 0 1
fine 1 2 0
good 0 1 2
使用value_counts + unstack
res = df[['column3', 'column2']].value_counts().unstack('column2', fill_value=0)
print(res)
Output
column2 medium short tall
column3
best 0 0 1
fine 1 2 0
good 0 1 2
作為替代groupby + unstack :
res = df.groupby(['column3', 'column2']).count().unstack('column2', fill_value=0)
print(res)
Output (groupby)
column1
column2 medium short tall
column3
best 0 0 1
fine 1 2 0
good 0 1 2
這兩種方法背后的想法是創建一個索引,然后將其拆開。 如果您想匹配問題中指定的相同順序,請先轉換為分類:
df['column2'] = pd.Categorical(df['column2'], categories=['tall', 'short', 'medium'], ordered=True)
res = df[['column3', 'column2']].value_counts().unstack('column2', fill_value=0)
print(res)
Output
column2 tall short medium
column3
best 1 0 0
fine 0 2 1
good 2 1 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.