Dawny33 Dawny33 - 1 year ago 141
Python Question

Reshaping a pandas dataframe

I have a Pandas dataframe which looks like this, where the columns are as follows:

  1. userid
    is as the name suggests

  2. name
    is each api event

  3. count
    is the frequency of the event

enter image description here

So, I want a dataframe which looks like this:

userid api_event_1 api_event_2 .... api_event_n
17 count_of_apievent1 count_of_apievent2 .... count_of_apieventn
2345 count_of_apievent1 count_of_apievent2 .... count_of_apieventn
. . . . .
. . . . .

Answer Source

What about

df.pivot(index='userid', columns='name', values='count_of_name')

Where df is your pandas dataframe (will insert Nan if some values do not exist. For example if there is no count for event A for user X)

For filling in 0instead of a NaN where the user do not have the api event, you can do this:

df.pivot(index='userid', columns='name', values='count_of_name').fillna(0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download