Home > OS >  how to update specific rows of a dataframe using a list of values?
how to update specific rows of a dataframe using a list of values?

Time:05-10

so guys, i have the following dataframe:dataframe

trough this line of code

df[df['cities'] == ''] 

i'm able to select the rows of the 'cities' column that are empty like this:filtered dataframe with only empty 'cities' rows

and i want to update all of this empty fields with a list of values, for example:

empty_cities_list = ['Des moines', 'Seattle', 'Des moines', ...] 

in the order of the list, like this:

df.iloc[729]['cities'] = empty_cities_list[0]
df.iloc[740]['cities'] = empty_cities_list[1]

df[729] and df[740] are just examples of rows in the original df that have empty values for cities. how can i do this? i've tried looping through df and empty_cities_list using zip but it didnt work. Just making the question simpler, how can i update specific rows of a dataframe passing a list of values to be used?

CodePudding user response:

You can try following if the length of empty_cities_list matches the '' num in cities column

df.loc[df['cities'] == '', 'cities'] = empty_cities_list

CodePudding user response:

Try this:

import numpy as np

df['col1'] = df['col1'].replace('', np.nan)
df['cities']= df['cities'].fillna(pd.Series(empty_cities_list, index=df[df['cities'].isnull()].index))

Explanation:

First, convert all your '' to NaN then replace the NaN values with empty_cities_list by using fillna(). df[df['cities'].isnull()].index this gets the indices of all columns which needs to be replaced.

  • Related