3

I need to delete the row if there is data missing in a certain column in the current row.

This is what I wrote:

for c, r in data.iterrows():
if (r['A'] == ""):
    data = data.drop(r)

But I receive the error:

"unreadable key error"

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • possible duplicate of [Pandas delete a row in a dataframe based on a value](https://stackoverflow.com/questions/53182464/pandas-delete-a-row-in-a-dataframe-based-on-a-value) – tidakdiinginkan Apr 17 '20 at 18:48
  • Though it wouldn't lead to an issue in this particular case, in general you should avoid making changes to the thing you iterate over: https://stackoverflow.com/questions/3346696/why-is-it-not-safe-to-modify-sequence-being-iterated-on (Also unnecessary to loop in this case) – ALollz Apr 17 '20 at 19:04

3 Answers3

1

You can do something like this:

data = data.drop(data[data['A'] == ''].index)

OR

data[data['A'] != ""]
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

Try this, using boolean indexing:

data[data['A'] != ""]

data['A'] != "" returns as bolean series that is True for each row where that value is not "".

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
0

For your part identation error if not you have to refer them as NaN values.

For dropping the data from a row if having null values. df = df[df['Column_name'].notna()]