0

I am trying to count how many values I have in a dataframe row subsection. Values are actually strings, so even counting 1 or -1 would not work. Hence I switched values to 'a', 'b' and 'c'. In example below:

COL1  COL2  COL3  COL4  'a'  'b'  'c' # last 3 columns abc would count occurences
a     c     b     c
c     a     a     b
c     b     c     b

I am however only trying to count values per row in subsection COL3 and COL4. I have looked at various answers at SO, including here. I also tried to include a subsection by the following:df1["a"] = df1[COL3:COL4], count(a) Thank you for your suggestions.

user1739581
  • 85
  • 2
  • 14

1 Answers1

2

Is this what you are after?

You can use value_counts to count element occurrences from column 3 and 3 and then concat the count df to the original df.

pd.concat([df,\
           df[['COL3','COL4']].apply(lambda x: x.value_counts(),axis=1).fillna(0)],\
           axis=1)
Out[13]: 
  COL1 COL2 COL3 COL4    a    b    c
0    a    c    b    c  0.0  1.0  1.0
1    c    a    a    b  1.0  1.0  0.0
2    c    b    c    b  0.0  1.0  1.0
Allen Qin
  • 19,507
  • 8
  • 51
  • 67