Rachel
Rachel

Reputation: 1967

Fill missing values error in pandas?

I have a dataframe with quite a few URLs. However, some are missing. It basically looks like this:

import pandas as pd
import numpy as np
csv = [{"url_1" : np.NaN, "url_2" : "https://www.mercedes-benz.de/content/germany/mpc/mpc_germany_website/de/home_mpc/passengercars/home/new_cars/models/mercedes_amg_gt/r190.html"}]

df = pd.DataFrame(csv)

In this case, url_1 is missing. I am trying to replace it with the entries in column url_2. This is what I do:

df.url_1 = df.url_1.fillna(df.url_2, inplace=True)

This is the result:

    url_1   url_2
0   None    https://www.mercedes-benz.de/content/germany/m...

I have two questions:

(1) Why isn't the missing value replaced?

(2) In the original data set I am thrown an error: invalid fill value with a <class 'pandas.core.frame.DataFrame'> The dataframe looks exactly the same - and I at least do not get an error in the small test I presented above. What does the error tell me and how do I get rid of it?

Any help is gladly appreciated! Thank you, /R

Upvotes: 2

Views: 848

Answers (2)

Thomas Grsp
Thomas Grsp

Reputation: 502

(1) & (2)

You can't use assignation and the keyword inplace

df['url_1'] = df['url_1'].fillna(df['url_2'])
# or
df['url_1'].fillna(df['url_2'], inplace=True)

This should solve both problems.

Upvotes: 1

jezrael
jezrael

Reputation: 863741

You need remove inplace if want assign output, because if inplace parameter function return None:

df.url_1 = df.url_1.fillna(df.url_2)
print (df)
                                               url_1  \
0  https://www.mercedes-benz.de/content/germany/m...   

                                               url_2  
0  https://www.mercedes-benz.de/content/germany/m...  

print (df.url_1.fillna(df.url_2, inplace=True))
None

Or dont assign and use inplace:

df.url_1.fillna(df.url_2, inplace=True)
print (df)
                                               url_1  \
0  https://www.mercedes-benz.de/content/germany/m...   

                                               url_2  
0  https://www.mercedes-benz.de/content/germany/m...  

Upvotes: 2

Related Questions