In my data, I have this column “price_range”.
Dummy dataset:
df = pd.DataFrame({'price_range': ['€4 - €25', '€3 - €14', '€25 - €114', '€112 - €146', 'No pricing available']})
I am using pandas. What is the most efficient way to get the upper and lower bound of the price range in seperate columns?
Advertisement
Answer
Alternatively, you can parse the string accordingly (if you want to limits for each row, rather than the total range:
df = pd.DataFrame({'price_range': ['€4 - €25', '€3 - €14', '€25 - €114', '€112 - €146']}) def get_lower_limit(some_string): a = some_string.split(' - ') return int(a[0].split('€')[-1]) def get_upper_limit(some_string): a = some_string.split(' - ') return int(a[1].split('€')[-1]) df['lower_limit'] = df.price_range.apply(get_lower_limit) df['upper_limit'] = df.price_range.apply(get_upper_limit)
Output:
Out[153]: price_range lower_limit upper_limit 0 €4 - €25 4 25 1 €3 - €14 3 14 2 €25 - €114 25 114 3 €112 - €146 112 146