2

I have a dataframe of comments from a survey. I want to export the dataframe as a csv file and remove the NaNs without dropping any rows or columns (unless an entire row is NaN, for instance). Here is a sample of the dataframe:

enter image description here

I don't care about maintaining the index so I'm, fine with just dropping individual cells with NaNs and shifting those column's rows up instead of dropping entire rows, so I'd just have a nice compressed output csv file without any empty cells.

hulio_entredas
  • 675
  • 1
  • 12
  • 1
    It's not a sample of your dataframe, it's an image. Try: `df = df.dropna(how='all', axis=1)` as the first part of your code. – Corralien Apr 13 '23 at 20:26
  • What are you replacing `NaN` with on the individual cells? – Marcelo Paco Apr 13 '23 at 20:27
  • I just want those cells to disappear without removing the entire row @MarceloPaco – hulio_entredas Apr 13 '23 at 20:29
  • Does this answer your question? [better way to drop nan rows in pandas](https://stackoverflow.com/questions/36370839/better-way-to-drop-nan-rows-in-pandas) – Jamiu S. Apr 13 '23 at 20:44
  • you're taking about exporting to csv. do they still show up as `NaN` in the exported csv because it shouldn't. other's have answered on how to drop rows containing all the NaNs – Asish M. Apr 13 '23 at 22:18
  • It doesn't show up as NaN, just empty cells. I'd like the empty cells to disappear on a cell-by-cell basis. – hulio_entredas Apr 14 '23 at 12:41

1 Answers1

1

To remove entire rows with all NaN you can use dropna():

df = df.dropna(how='all')

To remove NaN on the individual cell level in the data frame you can use fillna() by setting it to an empty string:

df = df.fillna("")
Marcelo Paco
  • 2,732
  • 4
  • 9
  • 26
  • Thanks. `df.dropna(how = 'all')` was good enough. Didn't exactly accomplish my goal of dropping individual cells with NaN but it was just fine for what I needed. – hulio_entredas Apr 14 '23 at 14:19