[英]Why is my apply pandas function turning every value into a None value?
I am trying to update the values in a dataframe column.我正在尝试更新数据框列中的值。 There are three different values in this column, which I will call Alive :
此列中有三个不同的值,我将其称为Alive :
Alive
-
And NaN values.和 NaN 值。
So I wrote a function to change the values:所以我写了一个函数来改变这些值:
def update_vals(x):
if x == "-":
x = False
if x == "NaN":
x = np.nan
else:
x = True
ppl_dataframe['Alive'].apply(update_vals)
However, when I go to apply this function, it simply results in None values being applied across the entire column.但是,当我去应用这个函数时,它只会导致在整个列中应用 None 值。
What am I doing wrong here?我在这里做错了什么?
your function doesn't have a return
statement so by default it returns None
, you could use:您的函数没有
return
语句,因此默认情况下它返回None
,您可以使用:
def update_vals(x):
if x == "-":
return False
if x == "NaN": # if you have np.nan values you have to change with np.isnan(x)
return np.nan
else:
return True
Do not use apply
, np.select
不要使用
apply
, np.select
import numpy as np
s1=ppl_dataframe['Alive']=='-'
s2=ppl_dataframe['Alive']=='NaN'
np.select([s1,s2],[False, np.nan],default=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.