Sergey  Gulbin Sergey Gulbin - 1 month ago 6
Python Question

Split one column data frame into a data frame with multiple columns

I'm working on the text file like this:

RES MON VOLUMEm3 FLOW_INcms FLOW_OUTcms
1 1 0.8099E+05 0.1115E-01 0.0000E+00
2 1 0.0000E+00 0.0000E+00 0.0000E+00
3 1 0.2435E+05 0.0000E+00 0.0000E+00


So, when I'm opening it as a data frame, in the output I got a dataframe with only one column

data = pd.read_csv(os.path.join(out, 'EDMA_1_rcp26_2025_1_output.rsv'), skiprows = 8, header = 0)


How can I split this data frame into multiple columns with columns names from the header?

Answer

Assuming that the values are separated by spaces, you can specify the optional delimiter parameter with regex:

pd.read_csv('test.txt', delimiter=r'\s+')

Using it on your dataset, I get:

   RES  MON  VOLUMEm3  FLOW_INcms  FLOW_OUTcms
0    1    1   80990.0     0.01115          0.0
1    2    1       0.0     0.00000          0.0
2    3    1   24350.0     0.00000          0.0
Comments