0

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?

  • 1
    are these lists or strings? are they always sorted or not? –  Apr 05 '22 at 16:36
  • They are a list of numbers, and not sorted. Just in min-max values –  Apr 05 '22 at 18:18
  • if it's already min-max format, then the linked duplicate should work for you. –  Apr 05 '22 at 18:36

1 Answers1

-2

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

Aidan Donnelly
  • 369
  • 5
  • 19