3

I have a long dataframe with only one column and around 800k rows. my data frame looks like this

54
53
53
53
53
...
0
0
0

So what I need is to count the number of occurrences of each value and saving this into a dataframe so the result would be something like this

54 1
53 1000
52 800
...
0 100000

I have tried using df.groupby(0) but it only returns an object. How can I get a two-column dataframe (or 1 column and a row-index showing the values)?

sbha
  • 9,802
  • 2
  • 74
  • 62
mshabeeb
  • 577
  • 2
  • 9
  • 25
  • 3
    use `df['column_name'].value_counts()` or `df['column_name'].value_counts().to_frame()` – Space Impact Nov 01 '18 at 11:21
  • 1
    Possible duplicate of [count the frequency that a value occurs in a dataframe column](https://stackoverflow.com/questions/22391433/count-the-frequency-that-a-value-occurs-in-a-dataframe-column) – vnthn Nov 01 '18 at 12:26

1 Answers1

9

Use value_counts and to_frame:

df = pd.DataFrame([1,2,4,5,5], columns=['values'])
df['values'].value_counts().to_frame().reset_index().rename(columns={'index':'values', 'values':'count'})

 values count
0   5   2
1   4   1
2   2   1
3   1   1
Franco Piccolo
  • 6,845
  • 8
  • 34
  • 52