1. 程式人生 > 其它 >pandas單列變多列

pandas單列變多列

[How to Split String Column in Pandas into Multiple Columns - Statology](https://www.statology.org/pandas-split-column/)

How to Split String Column in Pandas into Multiple Columns


You can use the following basic syntax to split a string column in a pandas DataFrame into multiple columns:

#split column A into two columns: column A and column B
df[['A', 'B']] = df['A'].str.split(',', 1, expand=True)

The following examples show how to use this syntax in practice.

Example 1: Split Column by Comma

The following code shows how to split a column in a pandas DataFrame, based on a comma, into two separate columns:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs, West', 'Spurs, West', 'Nets, East'],
                   'points': [112, 104, 127]})

#view DataFrame
df

	team	points
0	Mavs, West	112
1	Spurs, West	104
2	Nets, East	127

#split team column into two columns
df[['team', 'conference']] = df['team'].str.split(',', 1, expand=True)

#view updated DataFrame
df

	team	points	conference
0	Mavs	112	West
1	Spurs	104	West
2	Nets	127	East

Note that you can also reorder the columns after performing the split if you’d like:

#reorder columns
df = df[['team', 'conference', 'points']]

#view DataFrame
df

	team	conference points
0	Mavs	West	   112
1	Spurs	West	   104
2	Nets	East	   127

Example 2: Split Column by Other Delimiters

We can use the same syntax to split a column by other delimiters.

For example, we can split a column by a space:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs West', 'Spurs West', 'Nets East'],
                   'points': [112, 104, 127]})

#split team column into two columns
df[['team', 'conference']] = df['team'].str.split(' ', 1, expand=True)

#view updated DataFrame
df

	team	conference points
0	Mavs	West	   112
1	Spurs	West	   104
2	Nets	East	   127

We can also split a column by a slash:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs/West', 'Spurs/West', 'Nets/East'],
                   'points': [112, 104, 127]})

#split team column into two columns
df[['team', 'conference']] = df['team'].str.split('/', 1, expand=True)

#view updated DataFrame
df

	team	conference points
0	Mavs	West	   112
1	Spurs	West	   104
2	Nets	East	   127

Using this syntax, we can split a column by any delimiter we’d like.