'Separate and fill values between a Hyphen in a cell using Pandas into separate rows
I want to take a two column dataFrame that has values separated by hyphen that I want to split into its own row and fill in the missing values
Name: Value:
X1 5
X5-X7 3
X20 2
Desired dataFrame output
X1 5
X5 3
X6 3
X7 3
X20 2
Solution 1:[1]
I got it to work utilizing the following type of format, though I would be surprised if this was the best way to do it.
a, b = part.split("-")
key_Letter = [re.findall(r'(\w)(?:\d+)', a)[0]] #Pulls Letter not number
key_Letter = ' '.join(key_Letter) #converts list to str
a = [re.findall(r'(?:\w)(\d+)', a)[0]] #pulls the number from the first value
a = ' '.join(a)
b = [re.findall(r'(?:\w)(\d+)', b)[0]] #Pulls number from second value
b = ' '.join(b)
inter.extend(range(int(a), int(b) + 1)) #Creates the intermediate list with all the values, including those added between the a and b values
result = [key_RefDes + str(x) for x in inter] #will add back the letter to each value prior to outputting the result
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 | Skyler |
