G
G
gadzhi152016-08-15 22:38:31
Python
gadzhi15, 2016-08-15 22:38:31

Pandas how to split a column into multiple?

There is a Data frame with one column. In a column of record like 1,2,3. I created two more pillars. Now you need to extract the records by the separator (,) and write them, respectively, in the second and third columns. What is the best way to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-08-15
@gadzhi15

For example something like this:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'raw': ['1,2,3', '3,4,5']})

In [3]: df
Out[3]:
     raw
0  1,2,3
1  3,4,5

In [4]: df['col1']=df['raw'].str.split(',').str.get(0)

In [5]: df['col2']=df['raw'].str.split(',').str.get(1)

In [6]: df['col3']=df['raw'].str.split(',').str.get(2)

In [7]: df
Out[7]:
     raw col1 col2 col3
0  1,2,3    1    2    3
1  3,4,5    3    4    5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question