python - How to test for nan's in an apply function in pandas? -
i have simple apply
function execute on of columns. but, keeps getting tripped nan
values in pandas
.
input_data = np.array( [ [random.randint(0,9) x in range(2)]+['']+['g'], [random.randint(0,9) x in range(3)]+['g'], [random.randint(0,9) x in range(3)]+['a'], [random.randint(0,9) x in range(3)]+['b'], [random.randint(0,9) x in range(3)]+['b'] ] ) input_df = pd.dataframe(data=input_data, columns=['b', 'c', 'd', 'label'])
i have simple lambda this:
input_df['d'].apply(lambda acode: re.sub('\.', '', acode) if not np.isnan(acode) else acode)
and gets tripped nan values:
file "<pyshell#460>", line 1, in <lambda> input_df['d'].apply(lambda acode: re.sub('\.', '', acode) if not np.isnan(acode) else acode) typeerror: not implemented type
so, tried testing nan values pandas adds:
np.isnan(input_df['d'].values[0]) np.isnan(input_df['d'].iloc[0])
both same error.
i not know how test nan values other np.isnan
. there easier way this? thanks.
your code fails because first entry empty string , np.isnan
doesn't understand empty strings:
in [55]: input_df['d'].iloc[0] out[55]: '' in [56]: np.isnan('') --------------------------------------------------------------------------- typeerror traceback (most recent call last) <ipython-input-56-a9f139a0c5b8> in <module>() ----> 1 np.isnan('') typeerror: not implemented type
ps.notnull
work:
in [57]: import re input_df['d'].apply(lambda acode: re.sub('\.', '', acode) if pd.notnull(acode) else acode) out[57]: 0 1 3 2 3 3 0 4 3 name: d, dtype: object
however, if want replace use .str.replace
:
in [58]: input_df['d'].str.replace('\.','') out[58]: 0 1 3 2 3 3 0 4 3 name: d, dtype: object
Comments
Post a Comment