-3

I'm using panda data frame to handle data. Now I would need to aggregate data and wondering how.

I have df:

      Car  Country
0  Toyota    Japan
1  Nissan    Japan
2     BMW  Germany
3    Ford       US

I would like to create df2 with print:

Japan      2
Germany    1
US         1
martineau
  • 119,623
  • 25
  • 170
  • 301
Kenny_I
  • 2,001
  • 5
  • 40
  • 94
  • 2
    you are looking for [value_counts()](https://pandas.pydata.org/docs/reference/api/pandas.Series.value_counts.html#pandas.Series.value_counts) – luigigi Oct 26 '21 at 06:46
  • 2
    Does this answer your question? [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) – rftr Oct 26 '21 at 06:50
  • 2
    please don't call it 'panda' – sophocles Oct 26 '21 at 08:33

1 Answers1

1

As said by @luigigi, use pandas method value_counts

import pandas as pd
df = pd.DataFrame({"Car": ["Toyota", "Nissan", "BMW", "Ford"], "Country": ["Japan", "Japan", "Germany", "US"]})
df["Country"].value_counts()
hotuagia
  • 36
  • 3