2

As numpy.nan is a special float type in python, I have a pandas.Dataframe, some columns are consists of integer and Nan. As we know these columns will be float datatype. I want to copy pandas.Dataframe.to_csv , a CSV file,to database. But as those columns in CSV are float datatype (even I set 'object' type for them in pandas but they are converted to float when I do .to_csv), but the table schema of those columns in DB are the integer type, so they will be rejected to load in. this is the problem I got. what would be a good way to fix this? It's not good to change the DB schema.

for example I have a data = pandas.Dataframe() as below. ID: float Value: float

   ID       Value
0  1001.0    500.0 
1  1002.0    NaN
2  NaN       600.0
3  1003.0    800.0

How could I create a csv to be like this:

ID|Value
1001|500 
1002|
|600
1003|800
4
  • Format them as strings with zero decimals '{:0.0f}'.format(0.)? Commented Jun 2, 2017 at 17:37
  • Hey Thanks. Could you elaborate a little more? Commented Jun 2, 2017 at 17:58
  • Not without more data to play with. Give me some sample data and I'll show you what I'm talking about. You should read MCVE and HowToAsk Commented Jun 2, 2017 at 18:00
  • Sorry.I added a sample of data in the description. let me know if it works. Thanks! Commented Jun 2, 2017 at 18:13

1 Answer 1

1
df.to_csv('yourfile.csv', sep='|', index=None, float_format='%0.0f')

ID|Value
1001|500
1002|
|600
1003|800
Sign up to request clarification or add additional context in comments.

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.