0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please explain how the second option will work ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.