如何一次在多列上使用apply函数

时间:2018-04-16 17:49:40

标签: python pandas apply duration timedelta

是否可以在pandas中的多个列上调用apply函数,如果是,那么如何执行此操作...例如,

 df['Duration'] = df['Hours', 'Mins', 'Secs'].apply(lambda x,y,z: timedelta(hours=x, minutes=y, seconds=z))

This is what the expected output should look like once everything comes together

谢谢。

3 个答案:

答案 0 :(得分:1)

您应该使用:

df['Duration'] = pd.to_timedelta(df.Hours*3600 + df.Mins*60 + df.Secs, unit='s')

当您使用DataFrameaxis=1进行申请时,它是一个行计算,所以通常这种语法是有道理的:

df['Duration'] = df.apply(lambda row: pd.Timedelta(hours=row.Hours, minutes=row.Mins, 
    seconds=row.Secs), axis=1)

一些时间

import pandas as pd
import numpy as np
df = pd.DataFrame({'Hours': np.tile([1,2,3,4],50),
                  'Mins':  np.tile([10,20,30,40],50),
                  'Secs': np.tile([11,21,31,41],50)})

%timeit pd.to_timedelta(df.Hours*3600 + df.Mins*60 + df.Secs, unit='s')
#432 µs ± 5.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit df.apply(lambda row: pd.Timedelta(hours=row.Hours, minutes=row.Mins, seconds=row.Secs), axis=1)
#12 ms ± 67.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

与往常一样,申请应该是最后的手段。

答案 1 :(得分:0)

使用apply对数据框使用axis=1 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html

triangles = [{ 'base': 20, 'height': 9 }, { 'base': 10, 'height': 7 }, { 'base': 40, 'height': 4 }]

triangles_df = pd.DataFrame(triangles)

def calculate_area(row): return row['base'] * row['height'] * 0.5

triangles_df.apply(calculate_area, axis=1)

祝你好运!

答案 2 :(得分:0)

这可能会有所帮助。

import pandas as pd
import datetime as DT
df = pd.DataFrame({"Hours": [1], "Mins": [2], "Secs": [10]})
df = df.astype(int)

df['Duration'] = df[['Hours', 'Mins', 'Secs']].apply(lambda x: DT.timedelta(hours=x[0], minutes=x[1], seconds=x[2]), axis=1)

print(df)
print(df["Duration"])

<强>输出:

   Hours  Mins  Secs  Duration
0      1     2    10  01:02:10

0   01:02:10
dtype: timedelta64[ns]