MaxB
MaxB

Reputation: 458

Don't write values to excel sheet that are nan

I am trying to write only the rows who's value of a certain column is not equal to nan. An easy solution would be to drop all rows with the data equal to nan, however I am trying to preserve all of the data in the dataframe.

My thinking was to see if the value at the current location of a specific column was not equal to nan. If so then write that value out to the excel file.

if (df99.loc[df99['name'] != 'np.nan']):
    df99['name'].to_excel(writer, sheet_name = 'sheet1', startrow = 12, 
    startcol = 6, index=False, header=False )

If I have a dataframe with entries at indices 10, 20, and 30 I want it to still be written at the same indices in the excel sheet (11, 21, 31). I am trying to maintain their positions.

Upvotes: 1

Views: 1935

Answers (2)

ASH
ASH

Reputation: 20322

Ok, just do this.

# convert all nulls, NAN, etc., to 0
# don't want to deal with NANs...
df = df.fillna(0)
df


# count nulls in each field; 
# we're getting 0 so our results look good
df.isnull().sum()

Upvotes: 0

micric
micric

Reputation: 671

Create a copy of the original dataframe where you drop the Nans and then write that to Excel. That should do the work, if I'm not missing anything.

Upvotes: 1

Related Questions