Home > Software design >  How to add a new column based another column condition in pandas dataframe
How to add a new column based another column condition in pandas dataframe

Time:11-30

I have pandas dataframe like this.Count column is the number of regions where the base_path is present

region   base_path      count
us          /image      3
emea        /video      2
asia        /docs       2
emea        /image      3
us          /video      2
us          /docs       2
asia        /location   1
asia        /image      3

I want to get an additional column, which will put some some comment about the regions of the corresponding base path:

region   base_path      count   comment
us          /image      3       us-emea-asia
emea        /video      2       emea-us
asia        /docs       2       asia-us
emea        /image      3       us-emea-asia
us          /video      2       emea-us
us          /docs       2       asia-us
asia        /location   1       asia
asia        /image      3       us-emea-asia

How can I achieve this in efficient way?

CodePudding user response:

df['comment'] = df.groupby('base_path')['region'].transform('-'.join)

output:

  region  base_path  count       comment
0     us     /image      3  us-emea-asia
1   emea     /video      2       emea-us
2   asia      /docs      2       asia-us
3   emea     /image      3  us-emea-asia
4     us     /video      2       emea-us
5     us      /docs      2       asia-us
6   asia  /location      1          asia
7   asia     /image      3  us-emea-asia
  • Related