igotBAWS
igotBAWS

Reputation: 105

Replace values in a pandas column based on dictionary/mapping of indices

I have the dictionary

dicts: {0: '1969',  1: '1971',  2: '76'}

I also have the following df:

    Start date      End Date
0       w              a
1       A              2
2       B             NaN

Now I want to put the dictionary in place of one DataFrame column

df = df.replace({'Start date': dicts})

Result: Nothing changed :(

Expected:

    Start date      End Date
0       1969           a
1       1971           2
2       76            NaN

Upvotes: 4

Views: 3001

Answers (3)

jpp
jpp

Reputation: 164623

pd.DataFrame.replace replaces by value, not by index alignment. For the latter, one option is to pd.Index.map an index via a dictionary:

dicts = {0: '1969',  1: '1971',  2: '76'}
df['StartDate'] = df.index.map(dicts)

print(df)

  StartDate EndDate
0      1969       a
1      1971       2
2        76     NaN

If there are potentially unmapped indices, you can use fillna with a series:

df['StartDate'] = pd.Series(df.index.map(dicts)).fillna(df['StartDate'])

Upvotes: 1

Tanner Clark
Tanner Clark

Reputation: 691

You could also do something like this:

import pandas as pd
#Creating Your DataFrame
d= {'Start Date': ['w','a','b'],'End Date': ['a',2,'Nan']}
df=pd.DataFrame(data=d)

#Assigning Your Dict Column
dicts = {0: '1969',  1: '1971',  2: '76'}
df['Start Date']= pd.DataFrame.from_dict(dicts, orient='index')
df

See the documentation of dictionary to dataframe here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_dict.html

Upvotes: 1

cs95
cs95

Reputation: 402273

I think a more appropriate option here would be to convert your dict to a Series and call update:

df['Start date'].update(pd.Series(dct))
df

  Start date End Date
0       1969        a
1       1971        2
2         76      NaN

replace will not work, because it requires the dictionary to contain {: }, but you've provided the indices instead. So, in a nutshell, to have replace working, you would need to do

dct2 = {df.at[i, 'Start date']: v for i, v in dct.items()}
df.replace({'Start date': dct2})

  Start date End Date
0       1969        a
1       1971        2
2         76      NaN

Upvotes: 3

Related Questions