I have two columns with a list of each strings, named Tag 1, Tag 2. In simple terms I need to check if a string in a cell is found anywhere in the adjacent column and if so, swap the values.
Existing Dataframe
Tag 1 Tag 2
Amazon Twitter
Amazon Google
eBay Amazon
Reddit Facebook
Desired Output
Tag 1 Tag 2
Amazon Twitter
Amazon Google
Amazon eBay
Reddit Facebook
In the desired outcome, you can see that Amazon has switched places with eBay because it was found in the Tag 1 column.
Minimum Reproducible Example
import pandas as pd
data = {'Tag 1': ['Amazon', 'Amazon', 'eBay', 'Reddit'],
'Tag 2': ['Twitter', 'Google', 'Amazon', 'Facebook']}
df = pd.DataFrame(data)
I’ve been researching similar posts but can’t quite seem to get it exactly right.
Here’s my code so far.
3
Answers
This might do it
There are probably more efficient solutions, but I think this is pretty clear. It goes through each unique value in the ‘Tag 1’ column and checks for matches in the ‘Tag 2’ column, and swaps them. I was not sure if you wanted to avoid double swaps, but that can be removed if it’s not a requirement.
You could use
pd.where
to check when a value in ‘Tag 2’ existis in ‘Tag 1’ usingisin
and swap the values when you assign back:prints back:
The answer of @sophocles is probably the right.
However if you just want to reorder columns in a lexicographical order for instance, try: