2

I have a dataframe which is shown below: enter image description here

I want to drop out all the record that has nan in 'Rego' column. I tried few commands such as

temp_df = temp_df[temp_df['Rego'].notnull()]
temp_df = temp_df[temp_df['Rego'].notna()]
t = temp_df.loc[temp_df['Rego'].notnull()]
temp_df.dropna(axis=0,how='all')
temp_df.dropna(how ='any',inplace=True)

but none of the above commands drop nan values from the Rego column. I looked into existing threads of the forum (Can't drop NAN with dropna in pandas) but couldn't fix the issue. Could anyone guide me where am I making the mistake?

user2293224
  • 2,128
  • 5
  • 28
  • 52
  • 5
    That is likely not a `NaN`, it is a *string* with as content `nan`, so `'nan'`. You can filter that with `temp_df[temp_df['Rego'] != 'nan']`. – Willem Van Onsem Oct 16 '19 at 19:59
  • 1
    What is the output of `print(type(temp_df.iloc[4]['Rego']))`? You ideally want this to return `NoneType` if your data is correctly represented as a `NaN`. I suspect, as @WillemVanOnsem mentioned, you actually have the data stored as a `str` type which won't work with the `.dropna()` methods. – gbeaven Oct 16 '19 at 20:09
  • 2
    @WillemVanOnsem Thanks for pointing out the mistake. You guys are right its string. I just realized that. have been struggling to fix this issue for about two hours. Thanks for your prompt reply. – user2293224 Oct 16 '19 at 20:17
  • 1
    @gbeaven Thanks you saved my day. The output of print statement is . – user2293224 Oct 16 '19 at 20:18

2 Answers2

2

Maybe some nan value is a str, so do this first:

temp_df['Rego'].replace('nan',np.nan,inplace=True)

Then now you can do:

temp_df_fitered=temp_df[temp_df['Rego'].notnull()]
ansev
  • 30,322
  • 5
  • 17
  • 31
0

You didn't assign the result to a dataframe. This may work:

temp_df=temp_df.dropna(axis=0,how='all')
razimbres
  • 4,715
  • 5
  • 23
  • 50