我认为这是为了与NaN
s,因为:
np.nan != np.nan
so if NaN
它回来了NaN
s else 处理字符串。
Sample:
check = pd.DataFrame({'Id':[np.nan, '0909.0', '023', '09.06']})
a = check['Id'].map(lambda x: x if x != x else (str(x)[:str(x).rfind('.0')] if str(x).rfind('.0') != -1 else str(x)))
print (a)
0 NaN
1 0909
2 023
3 09
Name: Id, dtype: object
如果省略它它可以工作,因为转换为字符串,但第一个值不是np.nan
,但是字符串nan
:
a = check['Id'].map(lambda x: (str(x)[:str(x).rfind('.0')] if str(x).rfind('.0') != -1 else str(x)))
print (a)
0 nan
1 0909
2 023
3 09
Name: Id, dtype: object
如果所有值都是字符串NaN
s 并删除转换为字符串:
a = check['Id'].map(lambda x: ((x)[:(x).rfind('.0')] if (x).rfind('.0') != -1 else (x)))
print (a)
AttributeError:“float”对象没有属性“rfind”
a = check['Id'].map(lambda x: x if x != x else ((x)[:(x).rfind('.0')] if (x).rfind('.0') != -1 else (x)))
print (a)
0 NaN
1 0909
2 023
3 09
Name: Id, dtype: object