擅长:python、mysql、java
<p>下面是一个非常容易理解的答案,无需创建额外的列</p>
<pre class="lang-py prettyprint-override"><code>#Grouping by Studend Id and Name
new_df = df.groupby(['Student Id','Name'])['Marks'].apply(list).reset_index()
#Now, in the marks columns, we have a list (as seen below).
#So, we convert the list into different columns, using pd.Series
# Student Id Name Marks
# 0 id_1 John [112]
# 1 id_2 Rafs [181, 182, 183]
# 2 id_3 Juan [222, 312]
# 3 id_4 Roller [21]
temp_df = new_df['Marks'].apply(pd.Series)
#Now, this is all decorative stuff.
#Converting the column names from 0,1,2 to Marks1, Marks2, Marks3
temp_df.columns = list(map(lambda x: 'Marks'+str(x+1), temp_df.columns))
# Assigning this new temporary df to the original df
new_df[temp_df.columns] = temp_df
#Dropping the Marks column
final_df = new_df.drop('Marks',axis=1)
print(final_df)
</code></pre>
<p>输出:</p>
<pre><code> Student Id Name Marks1 Marks2 Marks3
0 id_1 John 112.0 NaN NaN
1 id_2 Rafs 181.0 182.0 183.0
2 id_3 Juan 222.0 312.0 NaN
3 id_4 Roller 21.0 NaN NaN
</code></pre>