[英]Apply function to every column of pandas dataframe without for loop?
I would like to convert a dataframe of timedeltas into hours. 我想将timedeltas的数据帧转换为小时。 I can do this for one series (one column of the dataframe) but I would like to find a way to apply it to all columns. 我可以针对一个系列(数据框的一列)执行此操作,但我想找到一种将其应用于所有列的方法。
A for loop
works, but is there a faster or more pythonic way to do this? for loop
可以工作,但是有没有更快或更Python的方法来做到这一点?
import pandas as pd
import datetime
import numpy as np
df = pd.DataFrame({'a': pd.to_timedelta(['0 days 00:00:08','0 days 05:05:00', '0 days 01:01:57']),
'b' : pd.to_timedelta(['0 days 00:44:00','0 days 00:15:00','0 days 01:02:00']),
'c': pd.to_timedelta(['0 days 00:34:33','0 days 04:04:00','0 days 01:31:58'])})
df
a b c
0 00:00:08 00:44:00 00:34:33
1 05:05:00 00:15:00 04:04:00
2 01:01:57 01:02:00 01:31:58
for c in df.columns:
df[c] = (df[c]/np.timedelta64(1,'h')).astype(float)
df
a b c
0 0.002222 0.733333 0.575833
1 5.083333 0.250000 4.066667
2 1.032500 1.033333 1.532778
I've tried using lambda, but there's something I'm getting wrong: 我已经尝试过使用lambda,但是出现了一些问题:
df = df.apply(lambda x: x/np.timedeltat(1, 'h')).astype(float)
Returns the error: 返回错误:
AttributeError: ("'module' object has no attribute 'timedelta'", u'occurred at index a')
Use np.timedelta64
working with all columns converted to 2d numpy array: 使用np.timedelta64
处理所有转换为2d numpy数组的列:
df = pd.DataFrame(df.values / np.timedelta64(1, 'h'), columns=df.columns, index=df.index)
print (df)
a b c
0 0.002222 0.733333 0.575833
1 5.083333 0.250000 4.066667
2 1.032500 1.033333 1.532778
If want use apply
: 如果要使用,请apply
:
df = df.apply(lambda x: x/np.timedelta64(1, 'h'))
print (df)
a b c
0 0.002222 0.733333 0.575833
1 5.083333 0.250000 4.066667
2 1.032500 1.033333 1.532778
Or total_seconds
: 或total_seconds
:
df = df.apply(lambda x: x.dt.total_seconds() / 3600)
print (df)
a b c
0 0.002222 0.733333 0.575833
1 5.083333 0.250000 4.066667
2 1.032500 1.033333 1.532778
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.