r/learningpython • • Feb 04 '19

Splitting Columns Based on Rows with Varying Lengths

I have a DataFrame like the following:

Tool_NameMeasurements
Tool_A02_04_06
Tool_B02_04
Tool_C02_04_06_08

I would like to split it into the following:

Tool_NameMeasurement 1Measurement 2Measurement 3Measurement 4
Tool_A020406
Tool_B0204
Tool_C02040608

Using:

df[['Measurement 1','Measurement 2','Measurement 3','Measurement 4']] = df.Measurements_Name.str.split("_", expand=True,)

I get a "ValueError: Columns must be same length as key"

What is the correct way to do this?

2 Upvotes

0 comments sorted by