pandas 在fillna中使用自定义函数Series

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

Using a custom function Series in fillna

pandas

提问by

Can I have a custom function for fill na in Pandas?

我可以在 Pandas 中有一个用于填充 na 的自定义函数吗?

s.fillna(lambda r: r["SomeColumn"]**2)

s.fillna(lambda r: r["SomeColumn"]**2)

I'd love to have a custom function that could take in the entire row itself.

我很想拥有一个可以容纳整行本身的自定义函数。

采纳答案by TomAugspurger

The valueargument can be a Series or DataFrame. So you could calculate the correct value ahead of time, and pass that to .fillna

value参数可以是一个系列或数据帧。所以你可以提前计算正确的值,然后将它传递给.fillna

fill_value = s['SomeColumn'] ** 2
s.fillna(fill_value)