I want to count distinct values per column (with pd.value_counts I guess) grouping data by some level in MultiIndex. The multiindex is taken care of with groupby(level= parameter, but apply raises a ValueError
Original dataframe:
>>> df = pd.DataFrame(np.random.choice(list('ABC'), size=(10,5)),
columns=['c1','c2','c3','c4','c5'],
index=pd.MultiIndex.from_product([['foo', 'bar'],
['w','y','x','y','z']]))
c1 c2 c3 c4 c5
foo w C C B A A
y A A C B A
x A B C C C
y A B C C C
z A C B C B
bar w B C C A C
y A A C A A
x A B B B A
y A A C A B
z A B B C B
What I want:
c1 c2 c3 c4 c5
foo A 4 2 0 3 2
B 1 2 2 1 2
C 0 1 3 1 1
bar A 4 1 0 1 2
B 0 2 2 1 1
C 1 2 3 3 2
I try to do:
>>> df.groupby(level=0).apply(pd.value_counts)
ValueError: could not broadcast input array from shape (5,5) into shape (5)
I can do it myself manually, but I think it must be a more obvious way.
groups = [g.apply(pd.value_counts).fillna(0) for n, g in df.groupby(level=0)]
index = df.index.get_level_values(0).unique()
correct_result = pd.concat(groups, keys=index) # THIS WORKS AS EXPECTED
I mean, this isn't that long to write, but I feel like I'm reinventing the wheel. Aren't this kind of operations done by groupby function?
Is there a more straightforward way of doing this, other than doing the split-apply-combine myself?
Use stack for MultiIndex Series, then SeriesGroupBy.value_counts and last unstack for DataFrame:
np.random.seed(123)
df = pd.DataFrame(np.random.choice(list('ABC'), size=(10,5)),
columns=['c1','c2','c3','c4','c5'],
index=pd.MultiIndex.from_product([['foo', 'bar'],
['w','y','x','y','z']]))
print (df)
c1 c2 c3 c4 c5
foo w C B C C A
y C C B C B
x C B A B C
y B A C A B
z C B A A A
bar w A B C A C
y A A B A B
x A A A C B
y B C C C B
z A A C B A
df1 = df.stack().groupby(level=[0,2]).value_counts().unstack(1, fill_value=0)
print (df1)
c1 c2 c3 c4 c5
bar A 4 3 1 2 1
B 1 1 1 1 3
C 0 1 3 2 1
foo A 0 1 2 2 2
B 1 3 1 1 2
C 4 1 2 2 1
来源:https://stackoverflow.com/questions/51799818/pandas-groupby-and-value-counts