0

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.

1
  • What do you define as an empty value? Just an empty string? Commented Sep 27, 2022 at 15:59

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

2 Comments

This will change the data type of the columns
@the_ordinary_guy true, but most likely won't matter if OP exports it to CSV. If it does, then don't use this solution.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.