0
t=pd.DataFrame(tree_pre)

t.fillna('',inplace=True)

print(t)

    0   1   2   3   4   5   6   7 8
0          55                      
1      30                  70      
2  25                  65      85  
3              60                  
4                  64  

but when I to txt

t.to_csv('2020.txt',sep='\t',index=False,header=False)

The txt have '.0'

    55.0                        
30.0                    70.0        

25.0 65.0 85.0
60.0
64.0

Why df have '.0' when I to txt?

disney82231
  • 189
  • 1
  • 3
  • 11
  • relevant: https://stackoverflow.com/questions/17092671/python-pandas-output-dataframe-to-csv-with-integers – kevh Jan 02 '20 at 15:37
  • Related: [Prevent pandas from automatically inferring type in read_csv](https://stackoverflow.com/questions/12101113/prevent-pandas-from-automatically-inferring-type-in-read-csv) – wwii Jan 02 '20 at 15:58

2 Answers2

5

You need to add a format specifier, this should work:

t.to_csv('2020.txt',sep='\t',index=False,header=False,float_format='%.0f')
marcos
  • 4,473
  • 1
  • 10
  • 24
1

Why df have '.0' when I to txt?

numpy.nan is an instance of float so any column with nan will be a float type.

>>> df = pd.DataFrame([[1,2,np.nan,np.nan,4],[4,np.nan,5,np.nan,6]])
>>> df
   0    1    2   3  4
0  1  2.0  NaN NaN  4
1  4  NaN  5.0 NaN  6

>>> df.dtypes
0      int64
1    float64
2    float64
3    float64
4      int64
dtype: object

>>> isinstance(df.iloc[0,2],float)
True

>>> df.fillna('',inplace=True)
>>> df.iloc[0,1]
2.0
>>> 
wwii
  • 23,232
  • 7
  • 37
  • 77