1

I have a dataframe with four columns (see input dataframe below). I need to check if Meta column has pattern relation (example M_relation) then add number from relation column to the meta column value. How this can be done in pandas?

Input dataframe:

--------------------------------------

    Entry1 | Relation | Entry2 | Meta |

--------------------------------------

E11 | R_31 | E21 | M_xxx |

E12 | R_21 | E51 | M_relation |

E33 | R_21 | E51 | M_yyy |

E44 | R_41 | E46 | M_relation |

Output:

--------------------------------------
Entry1 | Relation | Entry2 | Meta |
--------------------------------------

E11 | R_31 | E21 | M_xxx |

E12 | R_21 | E51 | M_relation_21 |

E33 | R_21 | E51 | M_yyy |

E44 | R_41 | E46 | M_relation_41 |
--------------------------------------
1
  • Can you show what you have tried? Commented Jun 21, 2016 at 1:15

1 Answer 1

1

You could:

pattern = df.Meta.str.split('_').str.get(1) == 'relation'
df.loc[pattern, 'Meta'] = df.loc[pattern, 'Meta'] + '_' + df.loc[pattern, 'Relation'].str.split('_').str.get(1)

to get:

  Entry1 Relation Entry2           Meta
0    E11     R_31    E21          M_xxx
1    E12     R_21    E51  M_relation_21
2    E33     R_21    E51          M_yyy
3    E44     R_51    E51  M_relation_51
Sign up to request clarification or add additional context in comments.

Comments

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.