我有一个数据框,如下所示:
one | two
a | 2 | 5
b | 3 | 6
NaN | 0 | 0
如何用字符串替换索引中的 NaN,例如“无标签”?
I tried:
df = df.replace(np.NaN, "No label")
and
df.index = df.index.replace(np.NaN, "No label")
But got
TypeError: expected string or buffer
您可以先将原始索引作为Series处理,然后重新分配索引:
import pandas as pd
import numpy as np
df = pd.DataFrame({'one': [2, 3, 0], 'two': [5, 6, 0]}, index=['a', 'b', np.nan])
df.index = pd.Series(df.index).replace(np.nan, 'No label')
print df
Output:
one two
a 2 5
b 3 6
No label 0 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)