1

I am new to pandas. Can someone help me in calculating frequencies of values for each columns.

Dataframe:

id|flag1|flag2|flag3|  
---------------------
1 |  1  |   2 |   1 |  
2 |  3  |   1 |   1 |  
3 |  3  |   4 |   4 |  
4 |  4  |   1 |   4 |  
5 |  2  |   3 |   2 |  

I want something like

id|flag1|flag2|flag3|  
---------------------
1 |  1  |   2 |   2 |  
2 |  1  |   1 |   1 |  
3 |  2  |   1 |   0 |  
4 |  1  |   1 |   2 |  

Explanation - id 1 has 1 value in flag1, 2 values in flag2 and 2 values in flag3.

2
  • why id 5 should be ignored? The last line could be 5|0|0|0 Commented Nov 20, 2017 at 11:01
  • id is not used, that is why it is ignored. values in column do not represent that they belong to a specific id, they represent numbers and I have to categorise on basis of those numbers Commented Nov 20, 2017 at 11:13

1 Answer 1

2

First filter only flag columns by filter or removing id column and then apply function value_counts, last replace NaNs to 0 and cast to ints:

df = df.filter(like='flag').apply(lambda x: x.value_counts()).fillna(0).astype(int)
print (df)
   flag1  flag2  flag3
1      1      2      2
2      1      1      1
3      2      1      0
4      1      1      2

Or:

df = df.drop('id', 1).apply(lambda x: x.value_counts()).fillna(0).astype(int)
print (df)
   flag1  flag2  flag3
1      1      2      2
2      1      1      1
3      2      1      0
4      1      1      2

Thank you, Bharath for suggestion:

df = df.filter(like='flag').apply(pd.Series.value_counts()).fillna(0).astype(int)
Sign up to request clarification or add additional context in comments.

1 Comment

Sir you dont need lambda here you can use pd.Series.value_counts, used here stackoverflow.com/questions/46863602/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.