'Python split row in two [duplicate]

I have the following data in a csv file:

name     height       width
apple    [180, 191]   [10, 20]
orange   [50, 130]    [15, 30]

How can I split height and width into: height-min, height-max, width-min, width-max using pandas?



Solution 1:[1]

You can try the following by splitting on the comma and removing your square brackets.

df['min-height'] = df['height'].str.split(',').str[0].str.strip('[]')
df['max-height'] = df['height'].str.split(',').str[1].str.strip('[]')
df['min-width'] = df['width'].str.split(',').str[0].str.strip('[]')
df['max-width'] = df['width'].str.split(',').str[1].str.strip('[]')

This will create new columns for you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aidan Donnelly