Issue
I have excel data that i modified to give me 1's = Filled cell 0's = Not Filled cell
Column A | Column B | Column C | Column D |
---|---|---|---|
1 | 0 | 1 | 0 |
1 | 1 | 1 | 0 |
0 | 0 | 1 | 0 |
0 | 0 | 1 | 0 |
what i want to get is:
Filled | Not Filled | |
---|---|---|
Column A | 2 | 2 |
Column B | 1 | 3 |
Column C | 4 | 0 |
Column D | 0 | 4 |
i tried:
summary_new = df2.groupby(['Column A','Column B','Column C','Column D'], as_index=False).agg(
FILLED_ITEM = pd.NamedAgg(column=([['Column A','Column B','Column C','Column D']]).astype(int), aggfunc=lambda x: x.eq(1).sum()),
NOT_FILLED_ITEM = pd.NamedAgg(column=([['Column A','Column B','Column C','Column D']]).astype(int), aggfunc=lambda x: x.eq(0).sum())).reset_index(drop=True)
Solution
Similarly to @Corralien's approach but as a single command, using sum
and eval
:
out = (df.sum().to_frame(name='Filled')
.eval('Not_Filled = @df.shape[1]-Filled')
)
Variant: .eval(...)
can be replaced by .assign(**{'Not Filled': lambda d: df.shape[1]-d['Filled']})
.
Output:
Filled Not_Filled
Column A 2 2
Column B 1 3
Column C 4 0
Column D 0 4
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.