Hi Abdellah
Kindly find the code snippet below for the required conversion:
import pandas as pd
# creating the sample dataset
raw_data = {'grade': ['A','A','A','B','B','B','B','C','C','C'],
'Num_Student': [3,6,5,8,8,6,4,5,6,7]}
df = pd.DataFrame(raw_data, columns = ['grade', 'Num_Student'])
#view the dataframe
print(df)
# Grouping the duplicate 'grades' and creating a list of 'Num_Student'
df = df['Num_Student'].groupby([df.grade]).apply(list).reset_index()
# expand df.Num_Student into its own dataframe
tags = df['Num_Student'].apply(pd.Series)
# rename each variable
tags = tags.rename(columns = lambda x : 'Num_Student_' + str(x))
# join the tags dataframe back to the original dataframe
df = pd.concat([df[:], tags[:]], axis=1)
# drop the 'Num_Student' columnn
df = df.drop('Num_Student',axis=1)
print(df)
Let me know if it does not work.
------------------------------
Ayush Jain
------------------------------
Original Message:
Sent: Sun November 08, 2020 11:40 AM
From: abdellah sadk
Subject: Data Manipulation Python
Hi community
please How could I group the attached table as shown in the picture?
thank you
------------------------------
abdellah sadk
------------------------------
#GlobalAIandDataScience
#GlobalDataScience