具有Nan支持的Pandas Lambda函数 [英] Pandas Lambda Function with Nan Support
问题描述
我正在尝试在Pandas中编写一个lambda函数,以检查Col1是否为Nan,如果是,则使用另一列的数据.我在获取代码(如下)以正确地编译/执行时遇到了麻烦.
I am trying to write a lambda function in Pandas that checks to see if Col1 is a Nan and if so, uses another column's data. I have having trouble getting code (below) to compile/execute correctly.
import pandas as pd
import numpy as np
df=pd.DataFrame({ 'Col1' : [1,2,3,np.NaN], 'Col2': [7, 8, 9, 10]})
df2=df.apply(lambda x: x['Col2'] if x['Col1'].isnull() else x['Col1'], axis=1)
有人对如何编写具有lambda函数的解决方案有什么好主意吗?还是我超出了lambda的能力?如果没有,您还有其他解决方案吗?谢谢.
Does anyone have any good idea on how to write a solution like this with a lambda function or have I exceeded the abilities of lambda? If not, do you have another solution? Thanks.
推荐答案
您需要 pandas.isnull
检查标量是否为NaN
:
df = pd.DataFrame({ 'Col1' : [1,2,3,np.NaN],
'Col2' : [8,9,7,10]})
df2 = df.apply(lambda x: x['Col2'] if pd.isnull(x['Col1']) else x['Col1'], axis=1)
print (df)
Col1 Col2
0 1.0 8
1 2.0 9
2 3.0 7
3 NaN 10
print (df2)
0 1.0
1 2.0
2 3.0
3 10.0
dtype: float64
但是更好的方法是使用 Series.combine_first
:
But better is use Series.combine_first
:
df['Col1'] = df['Col1'].combine_first(df['Col2'])
print (df)
Col1 Col2
0 1.0 8
1 2.0 9
2 3.0 7
3 10.0 10
使用 Series.update
的另一种解决方案:
Another solution with Series.update
:
df['Col1'].update(df['Col2'])
print (df)
Col1 Col2
0 8.0 8
1 9.0 9
2 7.0 7
3 10.0 10
这篇关于具有Nan支持的Pandas Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!