here we are!
Hi guys, I am trying to write a loop where: if the value of a cell in a specific column is 'None' and the value of the next cell is equal to an array of countries, then I change the 'None' value to the value of the next cell, that is the name of country.
I think I'm a bit off on solving this problem.
country_with_province = array(['Australia', 'China', 'France'], dtype=object)
for m in country_with_province:
if df5.loc[(df5['Province/State'] == None) and (df5['Country/Region'] == m):
df5.loc[:, "Province"] = df5.Province.fillna(m)
INPUT:
Date | Province/State | Country/Region | Recovered | Province | |
---|---|---|---|---|---|
0 | 22/01/2020 | None | Afghanistan | 0 | None |
1 | 22/01/2020 | None | Albania | 0 | None |
2 | 22/01/2020 | Tasmania | Australia | 0 | Tasmania |
3 | 22/01/2020 | None | Australia | 0 | None |
4 | 22/01/2020 | Hennan | China | 0 | Hennan |
5 | 22/01/2020 | Gansu | China | 0 | Gansu |
6 | 22/01/2020 | None | China | 0 | None |
7 | 22/01/2020 | Guineal French | France | 0 | Guineal French |
8 | 22/01/2020 | None | France | 0 | None |
OUTPUT:
Date | Province/State | Country/Region | Recovered | Province | |
---|---|---|---|---|---|
0 | 22/01/2020 | None | Afghanistan | 0 | None |
1 | 22/01/2020 | None | Albania | 0 | None |
2 | 22/01/2020 | Tasmania | Australia | 0 | Tasmania |
3 | 22/01/2020 | None | Australia | 0 | Australia |
4 | 22/01/2020 | Hennan | China | 0 | Hennan |
5 | 22/01/2020 | Gansu | China | 0 | Gansu |
6 | 22/01/2020 | None | China | 0 | China |
7 | 22/01/2020 | Guineal French | France | 0 | Guineal French |
8 | 22/01/2020 | None | France | 0 | France |
I working on aa df with 87552 rows.
Ty guys, I hope you can help me, I tried to search on stack, but I failed!
It seems like your method works fine, so... but you wanted something nicer
sel = df5['Province/State'].isnull() & df5['Country/Region'].isin(country_with_province)
df5.loc[sel, "Province"] = df5.loc[sel, 'Country/Region']
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.