Search code examples
pythonpandasdataframeexport-to-csvmissing-data

Pandas to_csv but remove NaNs on individual cell level without dropping full row or column


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.


Solution

  • 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("")