Sergey  Gulbin
Sergey Gulbin

Reputation: 311

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?

Upvotes: 2

Views: 107

Answers (1)

brianpck
brianpck

Reputation: 8254

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

Upvotes: 3

Related Questions