Home > OS >  How do I elegantly rename Pandas value counts output?
How do I elegantly rename Pandas value counts output?

Time:11-18

I want to call df['item'].value_counts() and, with minimal manipulation, end up with a dataframe with columns item and count.

I can do something like this:

df['item'].value_counts().reset_index().rename(columns={"item":"count", "index": "item"})

... which is fine but I'm like 95% sure there is a cleaner way to do this by passing a variable to reset_index or something similar

CodePudding user response:

import pandas as pd  # 1.5.1


df = pd.DataFrame({"item": list("aaabbbbbbccc")})

counts = df.groupby("item").value_counts().to_frame("count").reset_index()

print(counts)
  item  count
0    a      3
1    b      6
2    c      3

to_frame

CodePudding user response:

Let us try with groupby

df.groupby('item')['item'].count().reset_index(name='count')

CodePudding user response:

Using set_axis is very slightly cleaner. df['item'].value_counts().reset_index().set_axis(['item','count'], axis=1)

  • Related