简体   繁体   中英

How to impute the missing values depending on previous values?

I have data:

city        state    country Continent
Saint-Denis NaN      France  Europe
Saint-Denis NaN      NaN     Europe
Saint-Denis NaN      NaN     Europe
Kinshasa    NaN      NaN     Africa
Kinshasa    NaN      NaN     Africa

I am expecting to create the function which will analyze the similar cases and impute the country value of it.

I'm using the below code:

for i in range(0, len(df)):
    if df['city'][i] == 'Saint-Denis' and pd.isnull(df['country'].iloc[i]):
        df.country = 'France'
    else:
        pass

It is replacing the NaN , but not for specific city. It is replacing all NaN values.

You have a typo in your solution in the third line you are missing the index it should be like this df.country[i] = 'France' . Also, you can get the same result with pandas apply which should be faster:

df["country"] = df.apply(lambda x: "France" if (x.city=="Saint-Denis" and pd.isnull(x.country)) else x.country, axis=1)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM