Python 麻烦传入lambda来申请pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29155310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:09:00  来源:igfitidea点击:

Trouble passing in lambda to apply for pandas DataFrame

pythonpandasdataframelambdaapply

提问by sedavidw

I'm trying to apply a function to all rows of a pandas DataFrame (actually just one column in that DataFrame)

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

I'm sure this is a syntax error but I'm know sure what I'm doing wrong

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

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

The colcolumn contains a bunch a datetime.datetimeobjects and and d1is the earliest of them. I'm trying to get a column of the total number of seconds for each of the rows

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

EDITI keep getting the following error

编辑我不断收到以下错误

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

I don't understand why axisis getting passed to my lambdafunction

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

EDIT 2

编辑 2

I've also tried doing

我也试过做

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

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

And I get the same error

我得到了同样的错误

采纳答案by EdChum

Note there is no axisparam for a Series.applycall, as distinct to a DataFrame.applycall.

请注意,call没有axis参数,这与Series.applycall不同DataFrame.apply

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

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

There is one for a dfbut it's unclear how you're expecting this to work when you're calling it on a series but you're expecting it to work on a row?

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