Count instances of strings across multiple columns in pandas

Viewed 109

I've a df with this make-up. I'm interested in counting strings in the columns that have numerical names i.e. ['1','2','3','4','5']

enter image description here

And would return a series something like:

string1 10
strinsag1 5
strins1 5

...

I've tried various methods of groupby, Counter, and stack but I haven't found a solution that works

1 Answers

You can stack the data and use value_counts():

df[['1','2','3','4','5']].stack().value_counts()
Related