2

I would like to create a sunburst chart. Therefore, I need a dataframe with unique strings in every column (for every chart level). My aim is to ad an additional string to all strings in col2 that appear anywhere in col1. My original dataframe looks something like this:

   col1     col2    value
0   pig      dog        3
1   cat  chicken        2    
2  fish      pig        4
3   dog     mule        7

What I would like to achieve is something like this:

   col1     col2    value
0   pig   dog_ag        3
1   cat  chicken        2
2  fish   pig_ag        4
3   dog     mule        7

Any help is really appreciated.

2 Answers 2

3

Use Series.mask with Series.isin:

df['col2'] = df['col2'].mask(df['col2'].isin(df['col1']), df['col2'] + '_ag')
print (df)
   col1     col2  value
0   pig   dog_ag      3
1   cat  chicken      2
2  fish   pig_ag      4
3   dog     mule      7
Sign up to request clarification or add additional context in comments.

1 Comment

Jippie, now it works like a charm. Thank you heaps jezrael
0

Using list comprehension

df["col2"] = [i+"_ag" if i in df["col1"].values else i for i in df["col2"]]

Or you can use np.where

df["col2"] = np.where(df["col2"].isin(df["col1"]), df["col2"]+"_ag", df["col2"])

1 Comment

Thank you as well Sociopath. My first go to solve it myself was actually with np.where but I did not get to the combination with isin. Always great to see, for me as beginner, the flexibility of python.

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.