-1

I have a DataFrame with two columns 'A' and 'B'. My goal is to delete rows where 'B' is empty. Others have recommended to use df[pd.notnull(df['B'])]. For example here: Python: How to drop a row whose particular column is empty/NaN?

However, somehow this does not work in this case. Why not and how to solve this?

    A          B
0   Lorema     Ipsuma
1   Corpusa    Dominusa
2   Loremb     
3   Corpusc    Dominusc
4   Loremd     
5   Corpuse    Dominuse

This is the desired result:

    A          B
0   Lorema     Ipsuma
1   Corpusa    Dominusa
2   Corpusc    Dominusc
3   Corpuse    Dominuse
9
  • 1
    df.loc[df.ne('').all(1),:] Commented Jul 4, 2018 at 21:11
  • 1
    You have to understand what values are in your data frames. For example, if it is an empty string, df[df.B != ""] would do. If it is None or NA, then notnull() shoud lwork etc Commented Jul 4, 2018 at 21:12
  • Hint: try print(type(df['B'].iloc[2])) with your dataframe above to see what type you have. Commented Jul 4, 2018 at 21:14
  • @jpp: Thanks. I have done that and got: <class 'str'> Commented Jul 4, 2018 at 21:15
  • So you should try @Wen's solution.. looks like you have empty strings. Commented Jul 4, 2018 at 21:16

1 Answer 1

0

Basically, you could have whitespaces, tabs or even a \n in these blank cells.

For all those cases, you can strip values first, and then remove the rows, i.e.

df[df.B.str.strip().ne("") & df.B.notnull()]

I believe this should cover all cases.

Sign up to request clarification or add additional context in comments.

6 Comments

The empty cells are cells that have never been filled after the column was created. This solution does not yet do the trick. Would it be possible to fill those cells with something (e.g. '0') and then drop?
Can you post how you are creating this data frame? If a csv file, how you post a sample csv?
My function is complicated. But it either returns a string to the cell or it returns a white space ''. (So I expected your solution to work.) And then writes the result to a csv file.
Following jpps' suggestion, what does df['B'].iloc[2] == '' print ?
It prints False.
|

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.