I'm new at python. I struggle how to count how many people have died from each country. I use pandas dataframe. 0 - means that person died, 1 - survived. I have ~2000rows. Maybe it is not enough info, but I dont know how to solve this and from what exactly to start...
df['survived'] = df['survived'].replace(['no'], 0)
df['survived'] = df['survived'].replace(['yes'], 1)
countries_list = list(df['country'])
survived_list = list(df['survived'])
for i in range(len(survived_list)):
print(f'{survived_list[i]}: {countries_list[i]}')
I only get to this point and dont know what to do. With IF statement I also get nowhere: If i write like this (below) it shows me an error I dont know why. I hope that you get the idea what I want to do. Thank you in advance
if survived_list == 0:
0: United States 0: United States 0: United States 1: England 1: Norway 1: United States 0: France 1: France 1: Lebanon 1: Finland 0: Sweden 0: England ...
CodePudding user response:
"How to count how many people have died from each country" ?
Why do you get lists back from your dataframe to do some computations in pure Python ? Pandas dataframes are made for that kind of computation.
df["died"] = df["survived"].map(lambda x: 1 if x==0 else 0)
df.groupby(['country']).sum()
CodePudding user response:
Try this:
counts = {}
for i in range(len(survived_list)):
try:
counts[countries_list[i]] = survived_list[i]
except:
counts[countries_list[i]] = survived_list[i]