johnboy
johnboy

Reputation: 111

Replace dataframe row with dict

I have the following row from a dataframe:

print(row) =  

a      Nan
b      NaN
c      NaN
d      NaN
e      NaN

I have a dict: dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} that I want to replace this row with.

I tried a pandas series replace row = row.replace(dict1, regex=True) but it doesn't work. How can I do this?

Upvotes: 2

Views: 317

Answers (2)

Jeril
Jeril

Reputation: 8521

Can you try the following:

row.update(pd.Series(dict1.values(), index=dict1.keys()))

Upvotes: 1

jezrael
jezrael

Reputation: 863031

Use:

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

If want replace only index:

row.index = row.index.map(dict1.get)

Upvotes: 1

Related Questions