I currently have a data frame that is a reading a csv file called: "wimbledons_champions_claned.csv" I need to gather the data for the number of each unique nationality in the column "Champion Nationality". For example, nationality that shows up in the data is "AUS" and I need to count how many there are and I need to do this with 14 others. Is there any efficient way to do this without having to hard code. I currently am doing something like this:
import pandas as pd
df = pd.read_csv("wimbledons_champions_claned.csv")
ausChampions = len(df[df['Champion Nationality'] == 'AUS']
fraChampions = len(df[df['Champion Nationality'] == 'FRA']
etc...
If there is any better way to do this I would greatly appreciate it. Thank you.
Something like this:
df['Champion Nationality'].value_counts()
You could just use df['Champion Nationality'].value_counts()
. You could also use the Counter
function from the collections
library. This will return a dictionary of each Champion Nationality
in the pandas and how many times is repeated.
import pandas as pd
from collections import Counter
df = pd.read_csv("wimbledons_champions_claned.csv")
result = Counter(df['Champion Nationality'].tolist())
so if you do result['AUS']
it will return how many times it has shown up on the DataFrame
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.