将函数应用于 Pandas 数据框中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43163539/
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-09-14 03:19:50  来源:igfitidea点击:

Applying a function to a column in a pandas dataframe

pythonpandas

提问by mswhitehead

So I have a function replaceMonth(string), which is just a series of if statements that returns a string derived from a column in a pandas dataframe. Then I need to replace the original string with the derived one.

所以我有一个函数 replaceMonth(string),它只是一系列 if 语句,它返回从 Pandas 数据帧中的列派生的字符串。然后我需要用派生的字符串替换原始字符串。

The dataframe is defined like this:

数据框定义如下:

Index    ID        Year  DSFS           DrugCount
0        111111    Y1    3- 4 months    1

There are around 80K rows in the dataframe. What I need to do is to replace what is in column DSFS with the result from the replaceMonth(string) function.

数据框中大约有 80K 行。我需要做的是用 replaceMonth(string) 函数的结果替换列 DSFS 中的内容。

So if, for example, the value in the first row of DSFS was '3-4 months', if I ran that string through replaceMonth() it would give me '_3_4' as the return value. Then I need to change the value in the dataframe from the '3- 4 months' to '_3_4'.

因此,例如,如果 DSFS 第一行中的值是“3-4 个月”,如果我通过 replaceMonth() 运行该字符串,它将给我“_3_4”作为返回值。然后我需要将数据框中的值从“3-4 个月”更改为“_3_4”。

I've been trying to use apply on the dataframe but I'm either getting the syntax wrong or not understanding what it's doing correctly, like this:

我一直在尝试在数据框上使用 apply 但我要么语法错误,要么不明白它在做什么,就像这样:

dataframe['DSFS'].apply(replaceMonth(dataframe['DSFS']))

That doesn't ring right to me but I'm not sure where I'm messing up on it. I'm fairly new to Python so it's probably the syntax. :)

这对我来说并不合适,但我不确定我在哪里搞砸了。我对 Python 相当陌生,所以它可能是语法。:)

Any help is greatly appreciated!

任何帮助是极大的赞赏!

回答by piRSquared

When you applyyou pass the function that you want applied to each element.

当您apply传递要应用于每个元素的函数时。

Try

尝试

dataframe['DSFS'].apply(replaceMonth)

Reassign to the dataframe to preserve the changes

重新分配给数据框以保留更改

dataframe['DSFS'] = dataframe['DSFS'].apply(replaceMonth)