Home > Mobile >  String split data if condition from another column was met
String split data if condition from another column was met

Time:12-27

I have data formed like this:

| Company           | Value    |
| ------————————————| -------- |
| ABC Inc/BCD Corp. | 1        |
| CDE Inc./ABG Corp | 11       |
| AGB Inc/GHU Corp. | 5        |

And I want to split the Companies thru a ‘/‘ delimiter only if the Value is > 1.

Thus, the result should look like this:

| Company           | Value    |
| ------————————————| -------- |
| ABC Inc/BCD Corp. | 1        |
| CDE Inc.          | 11       |
| ABG Corp          | 11       |
| AGB Inc           | 5        |
| GHU Corp.         | 5        |

I already have a code for it, but it applies to all companies, not to companies with a value of more than 1.

CodePudding user response:

You can use str.split on Company column where Value > 1 and then use explode.

m = df['Value'] > 1
df.loc[m, 'Company'] = df.loc[m, 'Company'].str.split('/')
df = df.explode('Company')
print(df)




             Company  Value
0  ABC Inc/BCD Corp.      1
1           CDE Inc.     11
1           ABG Corp     11
2            AGB Inc      5
2          GHU Corp.      5
  • Related