pandas1.0将一个字符串的dtype列替换为NAN



我已经开始在pandas 1.0.1中使用StringDtype了。我知道这被认为是实验性的,但在对包含NaN的字符串类型的列使用替换时遇到了问题。

例如:

df = pd.DataFrame({'a': ['a', 'b', 'c', None]}, dtype='string')
df.replace({'c': 'e'})

以上结果为:

TypeError: Cannot compare types 'ndarray(dtype=object)' and 'str'

这可能是一个错误还是我做错了什么?

试试这个:

import pandas as pd
df = pd.DataFrame({'a': ['a', 'b', 'c', None]}, dtype='string')
df.replace('c', 'e', inplace=True)
print(df)

你会得到:

a
0     a
1     b
2     e
3  <NA>

或者,如果您想将dict保持为to_replace值,则首先需要:

list = ['a', 'b', 'c', np.nan]
s = pd.Series(list)
df = pd.DataFrame({'a': s})    
df.replace({'c': 'e'}, inplace=True)
print(df)

输出:

a
0    a
1    b
2    e
3  NaN

尽管已经阅读了文档,但无法真正解释它为什么有效。

最新更新