Home > Software design >  Converting Yes and No entries into columns
Converting Yes and No entries into columns

Time:06-24

So, if I have a data frame that has entries with strictly yes or no, like such:

- | A | B | C | D
1 | y | n | y | n
2 | n | y | n | y
3 | y | n | y | y
4 | n | y | n | n
5 | y | n | y | n
6 | y | n | y | n

Is there a pandas function that can convert the data frame such that, the yes and no becomes columns, where the entries show the counts of yes or no in A,B,C and D? Like such (these are random #s),

     - | y  | n
A | 11 | 12
B | 23 | 4
C | 43 | 13
D | 14 | 7

If not, what would be the best way to go about this? I'm aware that I can use pd.value_count() for each columnns manually, but I really hope there's a pandas function for this.

P.S. This is my first stack overflow question, sorry if I formatted the tables weirdly. Thanks!

CodePudding user response:

You can stack/unstack:

df.stack().groupby(level=1).value_counts().unstack()

output:

   n  y
A  2  4
B  4  2
C  2  4
D  4  2

used input:

   A  B  C  D
1  y  n  y  n
2  n  y  n  y
3  y  n  y  y
4  n  y  n  n
5  y  n  y  n
6  y  n  y  n

CodePudding user response:

Or, try to apply the pd.Series.value_counts method to each column of the dataframe then transpose with T results:

df.apply(pd.Series.value_counts).T

Output:

   n  y
A  2  4
B  4  2
C  2  4
D  4  2
  • Related