I have a Pandas DataFrame that has a column containing comma separated numbers, ampersand separated numbers, and hyphen separated number ranges...
Title LLFCs Red Amber Green
a 15, 18 11.65 2.86 1.89
b 16 & 19 9.08 2.93 1.53
c 112-114 6.45 2.54 1.64
I would like each 'LLFC' value to have its own row, meaning the numbers implied by the hyphen (113 in this case) must also be unwrapped. My ideal outcome is the following...
Title LLFCs Red Amber Green
a 15 11.65 2.86 1.89
a 18 11.65 2.86 1.89
b 16 9.08 2.93 1.53
b 19 9.08 2.93 1.53
c 112 6.45 2.54 1.64
c 113 6.45 2.54 1.64
c 114 6.45 2.54 1.64
I currently have the following few lines that do everything I require apart from unwrapping the hyphen values...
data1 = data1.assign(LLFCs=data1['LLFCs'].str.replace('-',', '))
data1 = data1.assign(LLFCs=data1['LLFCs'].str.replace(' & ',', '))
data1 = data1.assign(LLFCs=data1['LLFCs'].str.split(', ')).explode('LLFCs')
This code achieves the following...
Title LLFCs Red Amber Green
a 15 11.65 2.86 1.89
a 18 11.65 2.86 1.89
b 16 9.08 2.93 1.53
b 19 9.08 2.93 1.53
c 112 6.45 2.54 1.64
c 114 6.45 2.54 1.64
Which obviously doesn't include the hyphen wrapped value, would somebody be able to help me with this?