0

In a pandas dataframe, I have a string column with multiple values and I want to replace it one with based on a match for different rows.

enter image description here

Eg - Based on image, I want to specify "Extreme progressive,Progressive rock,Progressive" as Progressive, "Heavy,Bay area thrash" as Thrash, "Progressive death,Death,Progressive thrash" as Death and so on. How should I proceed with executing the same?

1 Answer 1

1

Using a dataframe:

dfa:

        ID No   Time    Variable    Val
0       123     0.1     A           1
1       123     0.1     B           2
2       123     0.1     C           3
3       127     0.8     A           4
4       127     0.8     B           5
5       127     0.8     C           6

you can create a dict mapper:

dictMapper = {'A' : 'aye', 'B': 'bee'}
dfa['Variable'] = dfa['Variable'].map(lambda x: dictMapper.get(x,x))
dfa:
    ID No   Time    Variable    Val
0   123     0.1     aye         1
1   123     0.1     bee         2
2   123     0.1     C           3
3   127     0.8     aye         4
4   127     0.8     bee         5
5   127     0.8     C           6

This ofcourse relies on you knowing all before: after combinations before updating your dataframe as you'll need an exact match to the dict keys.

Sign up to request clarification or add additional context in comments.

2 Comments

This works, though all the necessary combinations are required in this case. How to proceed if one decides on replacing the string with a word based on a match in the string?
you'll still need some form of maping dict I assume. You could split the string on commas and check each word against the dict.keys

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.