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 NaN
s with empty string and write to csv
df = df.fillna("")