我有数据框中的以下行:
print(row) =
a Nan
b NaN
c NaN
d NaN
e NaN
我有一个字典:dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
想要将此行替换为。
我尝试了 pandas 系列替换 row = row.replace(dict1, regex=True)
但它不起作用。我怎样才能做到这一点?
最佳答案
用途:
s = pd.Series(row.index.map(dict1.get), index=row.index)
print (s)
a 1
b 2
c 3
d 4
e 5
dtype: int64
如果只想替换索引:
row.index = row.index.map(dict1.get)
关于python - 用字典替换数据框行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260664/