1

I have a dataframe with empty values ​​in rows

enter image description here

How can I remove these empty values? I have already tried data.replace('', np.nan, inplace=True) and data.dropna() but that didn't change anything. What other ways are there to drop empty rows from a dataframe?

2
  • Have you tried dropna? Commented Mar 31, 2021 at 13:42
  • I tried it right away, but there was no effect Commented Mar 31, 2021 at 13:47

4 Answers 4

1

Try with

data = data.replace('', np.nan).dropna()

Update

data = data.apply(pd.to_numeric,errors='coerce').dropna()
Sign up to request clarification or add additional context in comments.

2 Comments

Tried it, no effect
@Sam324 is that empty or it is ' '?
1

As you have spaces in a numeric variable, I'm assuming it got read in as a string. The way I would solve this in a robust way is following the following:

data = {'lattitude': ['', '38.895118', '', '', '', '45.5234515', '', '40.764462'],
        'longitude': ['', '-77.0363658', '', '', '', '-122.6762071', '', '-11.904565']}
df = pd.DataFrame(data)

enter image description here

Change the fields to a numeric field. errors='coerce' will change the values it can not convert to a numeric to pd.NaN.

df = df.apply(lambda x: pd.to_numeric(x, errors='coerce'))

enter image description here

The only thing you'll have to do now is drop the NA's

df.dropna(inplace=True)

enter image description here

Another possible solution is to use regular expressions. In this case it's a negative match to any character. So if the field does not have a character, it'll be caught here. Of course there are multiple regex possible here.

mask = (df['lattitude'].str.contains(r'(^\S)') & df['longitude'].str.contains(r'(^\S)'))
df = df[mask]

Comments

0

suppose latitude is between -90 and 90.

data = data[data['latitude'] <= 90]

this should work, no matter they are Nan or ''

Comments

0

Try this. Hacky, but works.

data.fillna("").replace('', pd.NA, inplace=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.