Python pandas: output dataframe to csv with integers

后端 未结 6 674
囚心锁ツ
囚心锁ツ 2020-12-08 02:15

I have a pandas.DataFrame that I wish to export to a CSV file. However, pandas seems to write some of the values as float instead of int

6条回答
  •  醉梦人生
    2020-12-08 02:52

    The answer I was looking for was a slight variation of what @Jeff proposed in his answer. The credit goes to him. This is what solved my problem in the end for reference:

        import pandas
        df = pandas.DataFrame(data, columns=['a','b','c','d'], index=['x','y','z'])
        df = df.fillna(0)
        df = df.astype(int)
        df.to_csv('test.csv', sep='\t')
    

提交回复
热议问题