Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question