Reputation: 247
I have dataframe like this
Date Name
11-01-19 Craig-TX
22-10-23 Lucy-AR
I have dictionary
data ={'TX':'Texas','AR':'ARIZONA'}
I would like to replace partial string value from TX --> Texas and AR --> Arizona.
Resultant dataframe should be
Date Name
11-01-19 Craig-Texas
22-10-23 Lucy-Arizona
Do we have any specific function to replace the values in each row?
Upvotes: 0
Views: 103
Reputation: 323356
Adding regex = True
df=df.replace(data,regex=True)
Date Name
0 11-01-19 Craig-Texas
1 22-10-23 Lucy-ARIZONA
More safe, for example if the Name contain TX, using replace
will fail
df.Name=df.Name.str.split('-',expand=True).replace({1:data}).agg('-'.join)
0 Craig-Lucy
1 Texas-ARIZONA
dtype: object
Upvotes: 1