Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integers become floats when concatenating pandas dataframes

I have 2 pandas dataframes:

df1 is an empty dataframe: import pandas as pd import numpy as np

df1 = pd.DataFrame(columns=['Start','End','Duration'])

df1
Out[1]:
Empty DataFrame
Columns: [Start, End, Duration]
Index: []

df2 contains:

df2 = pd.DataFrame(np.array([None] * 3).reshape(-1,3),columns=['Start','End','Duration'])
df2['Start'] = 483
df2['End'] = 523
df2['Duration'] = 0.8

df2
Out[2]: 
       Start  End  Duration
    0    483  523      0.8


df2['Start']
Out[3]: 
0    483
Name: Start, dtype: int64

I want to concatenate the 2 dataframes:

df1= pd.concat([df1, df2], ignore_index=True)[df1.columns.tolist()]

df1 is:

df1
Out[4]: 
   Start    End  Duration
0  483.0  523.0    0.8

As you can see now "Start" and "End" are float... is there a way to keep them int?

like image 205
gabboshow Avatar asked Jun 27 '17 13:06

gabboshow


1 Answers

You can use astype:

df1[['Start','End']] = df1[['Start','End']].astype(int)

output:

   Start  End  Duration
0    483  523       0.8
like image 123
Tbaki Avatar answered Oct 17 '22 22:10

Tbaki