Python:将pandas dataframe中的系列对象列转换为int64 dtype

时间:2016-12-18 15:39:35

标签: python datetime pandas int series

我有以下数据框:

Day_Part     Start_Time    End_Time   
Breakfast    9:00          11:00
Lunch        12:00         14:00
Dinner       19:00         23:00

列Start_Time和End_time是'系列对象'马上。我想将这些列中的值转换为int64 dtype。

这就是我想要数据框的样子:

Day_Part     Start_Time    End_Time   
Breakfast    9             11
Lunch        12            14
Dinner       19            23

*非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以先转换to_timedelta,然后提取hour

df['Start_Time'] = pd.to_timedelta(df['Start_Time']+ ':00').dt.components.hours
df['End_Time'] = pd.to_timedelta(df['End_Time']+ ':00').dt.components.hours
print (df)

    Day_Part  Start_Time  End_Time
0  Breakfast           9        11
1      Lunch          12        14
2     Dinner          19        23

使用split并转换为int的另一种解决方案:

df['Start_Time'] = df['Start_Time'].str.split(':').str[0].astype(int)
df['End_Time'] = df['End_Time'].str.split(':').str[0].astype(int)
print (df)

    Day_Part  Start_Time  End_Time
0  Breakfast           9        11
1      Lunch          12        14
2     Dinner          19        23

使用extract并转换为int的解决方案:

df['Start_Time'] = df['Start_Time'].str.extract('(\d*):', expand=False).astype(int)
df['End_Time'] =  df['End_Time'].str.extract('(\d*):', expand=False).astype(int)
print (df)

    Day_Part  Start_Time  End_Time
0  Breakfast           9        11
1      Lunch          12        14
2     Dinner          19        23

转化为to_datetime的解决方案:

df['Start_Time'] = pd.to_datetime(df['Start_Time'], format='%H:%M').dt.hour
df['End_Time'] = pd.to_datetime(df['End_Time'], format='%H:%M').dt.hour
print (df)
    Day_Part  Start_Time  End_Time
0  Breakfast           9        11
1      Lunch          12        14
2     Dinner          19        23

<强>计时

#[300000 rows x 3 columns]
df = pd.concat([df]*100000).reset_index(drop=True)
print (df)

In [158]: %timeit pd.to_timedelta(df['Start_Time']+ ':00').dt.components.hours
1 loop, best of 3: 7.12 s per loop

In [159]: %timeit df['Start_Time'].str.split(':').str[0].astype(int)
1 loop, best of 3: 415 ms per loop

In [160]: %timeit df['Start_Time'].str.extract('(\d*):', expand=False).astype(int)
1 loop, best of 3: 654 ms per loop

In [166]: %timeit pd.to_datetime(df['Start_Time'], format='%H:%M').dt.hour
1 loop, best of 3: 1.26 s per loop