无法通过 lambda 来申请 pandas DataFrame

Trouble passing in lambda to apply for pandas DataFrame

我正在尝试将函数应用于 pandas DataFrame 的所有行(实际上只是该 DataFrame 中的一列)

我确定这是一个语法错误,但我知道我做错了什么

df['col'].apply(lambda x, y:(x - y).total_seconds(), args=[d1], axis=1)

col 列包含一堆 datetime.datetime 个对象,d1 是其中最早的一个。我正在尝试获取每一行的总秒数列

编辑 我一直收到以下错误

TypeError: <lambda>() got an unexpected keyword argument 'axis'

我不明白为什么 axis 会传递给我的 lambda 函数

编辑 2

我也试过

def diff_dates(d1, d2):
    return (d1-d2).total_seconds()

df['col'].apply(diff_dates, args=[d1], axis=1)

我得到同样的错误

注意 Series.apply call, as distinct to a DataFrame.apply 调用没有 axis 参数。

Series.apply(func, convert_dtype=True, args=(), **kwds)

func : function
convert_dtype : boolean, default True
Try to find better dtype for elementwise function results. If False, leave as dtype=object
args : tuple
Positional arguments to pass to function in addition to the value

df 有一个,但不清楚当您在系列中调用它时,您希望它如何工作,但您希望它连续工作?