Python 如何拆分数据帧,使其为每个拆分值创建一列

Python 如何拆分数据帧,使其为每个拆分值创建一列,python,pandas,split,dataframe,Python,Pandas,Split,Dataframe,例如 输入数据帧 Name Subjects Ramesh Maths,Science Rakesh MAths,Science,Social Studies John Social Science, Lietrature Name Subject1 Subject2 Subjects3 Ramesh Maths Science NaN Rakesh MAths Science

例如

输入数据帧

Name     Subjects
Ramesh   Maths,Science
Rakesh   MAths,Science,Social Studies
John     Social Science, Lietrature
Name     Subject1       Subject2     Subjects3
Ramesh   Maths          Science      NaN
Rakesh   MAths          Science      Social Studies
John     Social Science Literature   Nan
输出数据帧

Name     Subjects
Ramesh   Maths,Science
Rakesh   MAths,Science,Social Studies
John     Social Science, Lietrature
Name     Subject1       Subject2     Subjects3
Ramesh   Maths          Science      NaN
Rakesh   MAths          Science      Social Studies
John     Social Science Literature   Nan

您可以根据的结果创建新的df,然后执行以下操作:

您可以使用
pd.concat([df['Name'],df['Subjects'].str.split(',',,expand=True)],axis=1)