-1

I have to drop rows whose values is NaN in a specific Column ('Class') in my Dataframe. I know it is a common question and there is a lot of answers for it but I tried all ways that I found but nothing works. I tried it with:

dataset=dataset.dropna(axis='columns')

dataset = dataset[dataset['Class'].notna()]

dataset=dataset.dropna(subset=['Class'])

dataset = dataset[pd.notnull(dataset['Class'])]

I read the documentation on pandas website and tried all ways also the variant with df.dropna(how='any') but nothing is working and I still have the rows with the NaN values. Do anyone know why is that or how I can fix it?

MCore
  • 1
  • 3
  • Your code works... – U13-Forward Sep 12 '21 at 10:12
  • @U12-Forward I don't know what you mean but yes my code works. I actually already trained 107 Dataset for a research and I had many Problems withn NaN values but I solved it all with the lines of code in my post. But it is not working with this one. – MCore Sep 12 '21 at 10:53

1 Answers1

0

Maybe the NaN is a string of 'NaN' instead of np.NaN, check it out. happened to me once.

Niv Dudovitch
  • 1,614
  • 7
  • 15
  • Thanks for your answer! I thought about it too and tried: dataset = dataset.drop(dataset[dataset.Class =='NaN'].index) but it did not work. I also did export the dataset in csv file after pre processing to see how the nan values looks like. But the data is all correct and there is no NaN as String in it. The missing Values in the data are represented as '?' and I replaced it with 0. So I really don't know. – MCore Sep 12 '21 at 10:28
  • Why did you replace it with 0 and not with None? – Niv Dudovitch Sep 12 '21 at 10:53
  • I had to. I am doing a research and had to compare my work with former work whose missing values replaced with 0. So I actually must use the same conditions. I read about it, and it should not be soo bad for the training. – MCore Sep 12 '21 at 11:03
  • if you replace all missing values with 0, you will don't have None to drop.. – Niv Dudovitch Sep 12 '21 at 11:32