1

I am working with multiple big data frames. I want to remove their NaN parts automatically to ease the data cleansing process. Data is collected from a camera or radar feed, but the part of the data I need is when a specific object comes into the view horizon of the camera/ radar. So, the data file (frame) looks like below, and has lots of NaN values:

    total in seconds    datetime(utc)   channels    AlviraPotentialDronePlots_timestamp AlviraPotentialDronPlot_id  ...
0   1601381457  2020-09-29 12:10:57 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1   1601381459  2020-09-29 12:10:59 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2   1601381460  2020-09-29 12:11:00 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
3   1601381461  2020-09-29 12:11:01 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4   1601381463  2020-09-29 12:11:03 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... Useful data is here ... ... ... ... ... ... ... ... ...
623 1601382249  2020-09-29 12:24:09 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
624 1601382250  2020-09-29 12:24:10 NaN NaN NaN NaN NaN NaN NaN NaN ... 51.521264   5.858627    5.0 NaN NaN SearchRadar 0.0 0.0 NaN NaN
625 1601382251  2020-09-29 12:24:11 NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

I have removed the columns with all NaN values using:

df = df.dropna(axis=1, how='all')

Now, I want to remove rows that contain all NaN. However, since total in seconds and datetime(utc) are always present in the file, I cannot use the following command:

df = df.dropna(axis=0, how='all')

Also, I cannot use how='any', because that would remove parts of the useful data too (the useful data contains some NaN values which I will fill later). I have to use the dropna() in a way that it does not take the total in seconds and datetime(utc) into account, but if all other fields are NaNs, then removes the whole row.

The closest I came to solving this problem was the command mentioned in this link, but I guess I am not enough familiar with Python to be able to formulate the following logic:

  • if in one row field != [is not] 'total in seconds' | [or] 'datetime(utc)' & [and] other fields == [is] 'NaN' then remove the row

I tried writing this with for loop too, but I was not successful. Can someone help me with this?

Thanks in advance.

RFAI
  • 459
  • 4
  • 17

2 Answers2

1

You can check all columns without total in seconds, datetime(utc) by subset parameter with Index.difference:

cols = ['total in seconds','datetime(utc)']
checked = df.columns.difference(cols)

df = df.dropna(subset=checked, how='all')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

If your number of columns is constant, you can use the parameter thresh. Lets say you have 50 columns, you could put the thresh at 48 if you have 2 columns that are never empty. For more, check https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html

Odhian
  • 351
  • 5
  • 14