0

I am trying to replace the values of a Dataframe.

The DataFrame

data =[["China", 1436323776],["Brazil", 212559417],["Germany", 83992949],["Egypt", 102334404],["Chile", 19116201],["Australia", 25499884],["Venezuela", 28435940],["Netherlands", 17915568],["Belgium", 11589623]]

df = pd.DataFrame(data, columns = ["Country", "Population"])



    Country Population
0   China       1436323776
1   Brazil      212559417
2   Germany     83992949
3   Egypt       102334404
4   Chile       19116201
5   Australia   25499884
6   Venezuela   28435940
7   Netherlands 17915568
8   Belgium     11589623

What i tried is the following:

df.loc[df["Population"] >= 100000000, "Population"] = "large"
df.loc[(df["Population"] >= 20000000) & (df["Population"] < 100000000), "Population"] = "medium"
df.loc[df["Population"] < 20000000, "Population"] = "small"

Which works fine if i only run 1 line of it by itself, but when i try to let it run one after another, i get an error since now, not every cell has an int anymore, but also a String, and it cant compare str and int ( obviously). As seen here:

Country Population
0   China       large
1   Brazil      large
2   Germany     83992949
3   Egypt       large
4   Chile       19116201
5   Australia   25499884
6   Venezuela   28435940
7   Netherlands 17915568
8   Belgium     11589623

So i might have to iterate over each line, check my 3 conditions and then change the value, but i dont know how to do that.

Thanks for your help.

0 Answers0