Home > database >  How to set values as empty in dataframe using python
How to set values as empty in dataframe using python

Time:09-28

I tried to set some values in a dataframe to empty based on a condition in python. The code is something like this,

df.loc[df['Col1'] > 0.5, 'Col2'] = np.nan

However, in output file (.csv), it's "nan" there. So wondering if there are any ways to make it empty? Thanks.

CodePudding user response:

df.loc[df['Col1'] > 0.5, 'Col2'] = ""

... will give you an empty value of type string. If you export your DataFrame to a .csv file, you will get an empty value.

CodePudding user response:

After your step:

df = df.loc[df['Col1'] > 0.5, 'Col2'] = np.nan

fill NaNs with empty string and write to csv

df = df.fillna("")
  • Related