连接pandas数据帧时整数成为浮点数

时间:2017-06-27 13:10:49

标签: python pandas dataframe

我有2个pandas数据帧:

df1是一个空数据帧: 将pandas导入为pd 导入numpy为np

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

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

df2包含:

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

我想连接2个数据帧:

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

df1是:

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

正如你现在所看到的,“开始”和“结束”都是浮动的......有没有办法让它们保持整合?

1 个答案:

答案 0 :(得分:1)

您可以使用astype

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

输出:

   Start  End  Duration
0    483  523       0.8