1

I currently have a Dataframe that looks something like this:

enter image description here

The unique values are ['Somewhat Interested', 'Not at all Interested', nan,'Very Interested']

How would I go about creating a new dataframe that would have the same columns as above but for the index values 'Somewhat Interested', 'Not at all Interested', nan,'Very Interested' and the values inside the cell are the counts of each type of response. Im thinking a pivot table might do the trick but Im not sure.

What I want

In person meet ups alumni webinars alumni webinars etc...
Some what interested 24 32 12
Not interested 32 42 4
very intersted 21 31 53

2 Answers 2

1

Combine value_counts with apply to do it per column:

df.apply(pd.value_counts)
Sign up to request clarification or add additional context in comments.

2 Comments

pd.value_counts instead of pd.Series.value_counts? Nice! Didn't know about that one. (+1 soon!) :)
pd.Series.value_counts is simply a reference to pd.value_counts . Series module does not contain a definition for value_counts, it is defined in core.algorithms and is referenced in core. DataFrame.value_counts is a different thing though.
0

Actually, you can just apply pd.Series.value_counts for each column:

counts = df.fillna('NaN').apply(pd.Series.value_counts).fillna(0).astype(int)

1 Comment

@JeStra does this not solve your problem?

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.