如何在 pandas 中的应用函数中测试 nan?
How to test for nan's in an apply function in pandas?
我有一个在某些列上执行的简单 apply
函数。但是,它一直被 pandas
中的 NaN
值绊倒。
input_data = np.array(
[
[random.randint(0,9) for x in range(2)]+['']+['g'],
[random.randint(0,9) for x in range(3)]+['g'],
[random.randint(0,9) for x in range(3)]+['a'],
[random.randint(0,9) for x in range(3)]+['b'],
[random.randint(0,9) for x in range(3)]+['b']
]
)
input_df = pd.DataFrame(data=input_data, columns=['B', 'C', 'D', 'label'])
我有一个像这样的简单 lambda:
input_df['D'].apply(lambda aCode: re.sub('\.', '', aCode) if not np.isnan(aCode) else aCode)
它被 NaN 值绊倒了:
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 for this type
所以,我尝试只测试 Pandas 添加的 nan 值:
np.isnan(input_df['D'].values[0])
np.isnan(input_df['D'].iloc[0])
两者都得到相同的错误。
我不知道如何测试 np.isnan
以外的 nan 值。有没有更简单的方法来做到这一点?谢谢。
您的代码失败,因为您的第一个条目是空字符串,并且 np.isnan
不理解空字符串:
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 for this type
ps.notnull
有效:
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
但是,如果您只想替换某些东西,那么只需使用 .str.replace
:
In [58]:
input_df['D'].str.replace('\.','')
Out[58]:
0
1 3
2 3
3 0
4 3
Name: D, dtype: object
我有一个在某些列上执行的简单 apply
函数。但是,它一直被 pandas
中的 NaN
值绊倒。
input_data = np.array(
[
[random.randint(0,9) for x in range(2)]+['']+['g'],
[random.randint(0,9) for x in range(3)]+['g'],
[random.randint(0,9) for x in range(3)]+['a'],
[random.randint(0,9) for x in range(3)]+['b'],
[random.randint(0,9) for x in range(3)]+['b']
]
)
input_df = pd.DataFrame(data=input_data, columns=['B', 'C', 'D', 'label'])
我有一个像这样的简单 lambda:
input_df['D'].apply(lambda aCode: re.sub('\.', '', aCode) if not np.isnan(aCode) else aCode)
它被 NaN 值绊倒了:
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 for this type
所以,我尝试只测试 Pandas 添加的 nan 值:
np.isnan(input_df['D'].values[0])
np.isnan(input_df['D'].iloc[0])
两者都得到相同的错误。
我不知道如何测试 np.isnan
以外的 nan 值。有没有更简单的方法来做到这一点?谢谢。
您的代码失败,因为您的第一个条目是空字符串,并且 np.isnan
不理解空字符串:
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 for this type
ps.notnull
有效:
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
但是,如果您只想替换某些东西,那么只需使用 .str.replace
:
In [58]:
input_df['D'].str.replace('\.','')
Out[58]:
0
1 3
2 3
3 0
4 3
Name: D, dtype: object