[英]Count number of times each item in list occurs in a pandas dataframe column with comma separates values with additional aggregation of other columns
[英]Count number of values of multiple columns per each column and additional category column
我有一個數據框,其中包含多個帶有 0 和 1(A、B)的列以及一列(C)指示行的類別。 現在,我想計算每列和類別的 0 和 1 值。
import pandas as pd
test_data = {'A': [0,0,1,1,1,0],
'B': [0,1,0,1,0,1],
'C': ['a','a','b','b', 'c', 'c']}
df = pd.DataFrame(test_data)
我試圖弄清楚如何使用 pd.piovt_table 重新排列數據幀,但是我沒有成功獲得正確的轉換。 我嘗試了以下方法:
table = pd.pivot_table(df, columns = ['C'], index=['A'], aggfunc='count')
print('0', table)
這將導致以下輸出:
0 B
C a b c
A
0 2.0 NaN 1.0
1 NaN 2.0 1.0
我的目標是獲得以下輸出:
0 B | A # columns A and B
C a a b b c c | a a b b c c # row category based on C
0 1 0 1 0 1 | 0 1 0 1 0 1 # 0 and 1 values of the columns A and B
1 1 1 1 1 1 | 2 0 0 2 1 1 # counts
[編輯]或以下輸出:
0 B | A # columns A and B
C a b c | a b c # row category based on C
0| 1 1 1 | 2 0 1
1| 1 1 1 | 0 2 1
有人可以幫我解決這個問題嗎? 謝謝!
我認為你以前需要DataFrame.melt
第一種情況是第二種情況,使用 unstack()
new_df = (df.melt('C')
.groupby(['variable','C'])['value']
.value_counts().unstack(fill_value=0)
.stack()
.to_frame().T
.rename_axis(index=None,columns=[0,'C',None])
.sort_index(axis=1, ascending=[False,True,True]))
print(new_df)
0 B A
C a b c a b c
0 1 0 1 0 1 0 1 0 1 0 1
0 1 1 1 1 1 1 2 0 0 2 1 1
第二種情況它是第一個使用 stack()
new_df = (df.melt('C').groupby(['C','variable'])['value']
.value_counts().unstack(['variable','C'],fill_value=0)
.sort_index(axis=1, ascending=[False, True])
.rename_axis(columns=[0,'C'],index=None))
print(new_df)
或者
new_df = (df.melt('C')
.pivot_table(columns=['variable','C'],
index='value',
aggfunc='size',
fill_value=0)
.rename_axis(index=None, columns=[0,'C'])
.sort_index(axis=1, ascending=[False, True]))
輸出
0 B A
C a b c a b c
0 1 1 1 2 0 1
1 1 1 1 0 2 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.