Search code examples
pythonpandaspivotaggregate

Python: extract column from pandas pivot


I have a pivoted table total_chart = df.pivot_table(index="Name", values="Items", aggfunc='count') The output gives

A              8
B             52
C             24
D              6
E             43
F              5
G             13
I              1

I trying to get only the second column (number only) Is there any simple way to get it?


Solution

  • The code below should do the trick for you. It counts "Items", sort it ascending by the index "Name" and output just the counts without the index.

    df['Items'].value_counts().sort_index(ascending=True).tolist()