我在pandas中有一个数据帧(df_logs)如下:
name phone_number call_duration
Ana 4455533422 38
Mark 4452341115 300
Susan 33489982 120
call_duration列以秒表示,我想将此列中的所有值转换为分钟。
要做到这一点,我想使用timedelta对象但是我不知道如何传递值列表而不是一个给定的秒数。
而不是:
time = str(datetime.timedelta(seconds = 300))
我试图在我的' call_duration'中的每个值都没有成功。像这样的专栏:
df_logs = df_logs['call_duration'].values.astype(int)
time = str(datetime.timedelta(seconds = [i for i in df_logs]))
time = str(datetime.timedelta(seconds = *df_logs))
我想知道如何转换我的' call_duration'从几秒到几分钟。
谢谢。
答案 0 :(得分:2)
IIUC:
In [77]: df['new'] = pd.to_timedelta(df.call_duration, unit='s')
In [78]: df
Out[78]:
name phone_number call_duration new
0 Ana 4455533422 38 00:00:38
1 Mark 4452341115 300 00:05:00
2 Susan 33489982 120 00:02:00
In [79]: df.dtypes
Out[79]:
name object
phone_number int64
call_duration int64
new timedelta64[ns] # <----
dtype: object
<强>更新强>
In [93]: df['new'] = (df.call_duration/60).round().astype(int)
In [94]: df
Out[94]:
name phone_number call_duration new
0 Ana 4455533422 38 1
1 Mark 4452341115 300 5
2 Susan 33489982 120 2
<强> UPDATE2:强>
In [109]: df
Out[109]:
name phone_number call_duration
0 Ana 4455533422 38
1 Mark 4452341115 300
2 Susan 33489982 120
3 Max 123456789 564
In [110]: df['new'] = np.ceil(df.call_duration/60).astype(int)
In [111]: df
Out[111]:
name phone_number call_duration new
0 Ana 4455533422 38 1
1 Mark 4452341115 300 5
2 Susan 33489982 120 2
3 Max 123456789 564 10