M.Throw M.Throw - 9 days ago 6
Python Question

Using Python Panda's to fill new table with NaN values

I've imported data from a csv file which has columns NAME, ADDRESS, PHONE_NUMBER.
Sometimes, at least 1 of the columns has a missing value for that row. e.g

0 - Bill, Flat 2, 555123
1 - Katie, NaN, NaN
2 - Ruth, Flat 1, ?


I'm trying to get the NaN values to fill a new table which I can do if a filler value has been put in such as:
newDetails = details [details['PHONE_NUMBER']=="?"]

which gives me:

2 - Ruth, Flat 1, ?


I tried to use
fillna
but I couldn't find the syntax that would work.

Answer

Pandas fillna (pandas.DataFrame.fillna) is quite simple. Suppose your data frame is df. Here's how you can do.

df.fillna('_missing_value_', inplace=True)

It looks like you have different fields with missing value. May be try this:

df = df.where((pd.notnull(df)),'_missing_value_')

Edit1 to replace in a column

If you want to replace a column Flat 2, here's how:

col_flat = df[['Flat 2']].fillna('?')
df['Flat 2'] = col_flat['Flat 2']