如何将 pandas 中的字符串列拆分为多列
您可以使用以下基本语法将 pandas DataFrame 中的字符串列拆分为多个列:
#split column A into two columns: column A and column B df[[' A ',' B ']] = df[' A ']. str . split (' , ', 1 , expand= True )
以下示例展示了如何在实践中使用此语法。
示例 1:用逗号分隔列
以下代码显示了如何根据逗号将 pandas DataFrame 中的列拆分为两个单独的列:
import pandas as pd #createDataFrame 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
请注意,如果需要,您还可以在执行拆分后重新排列列:
#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
示例 2:通过其他分隔符拆分列
我们可以使用相同的语法用其他分隔符来划分列。
例如,我们可以将一列除以一个空格:
import pandas as pd #createDataFrame 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
您还可以用斜杠分隔列:
import pandas as pd #createDataFrame 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
使用此语法,我们可以按所需的任何分隔符拆分列。
其他资源
如何向 Pandas DataFrame 添加行
如何将 Numpy 数组添加到 Pandas DataFrame
如何计算 Pandas DataFrame 中的行数