0

I am trying to put a dataframe into a csv file and then access it. But by doing so the NaN values are showing up and the data are misaligned.

expenses = df.iloc[:][['Description','Debit']].dropna()

Here, i am trying to get the Description and debit column where there is no NaN data.

expenses.to_csv(path_or_buf= 'expenses.csv',index=False, header=["Description", "Debit"],encoding='utf-8')
expenses = pd.read_csv("expenses.csv", thousands=",", header= 0)

And then saving that dataframe into expenses and trying to access the same csv file but the content is misaligned. When I open the csv file its correct.

Here, is a screenshot to help you understand:

Image

1 Answer 1

2

That is because, by default the inplace=False, when you use dropna , do:

df.dropna(subset=['Description','Debit'], inplace=True) 

It should work. Your df, in this case, should have dropped the NaN values on these two columns.

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

2 Comments

I tried the same but it is making "expenses" dataframe into NoneType. And unable to use the to_csv method further down the line.
this way, the original dataframe itself is changed, and you can do, df.to_csv() directly, if you could share some sample rows, I can give you the working example.

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.