Reputation: 11
I have a pandas dataframe like this
StudentID Grades
101 A
101 C
101 B
102 B
102 C
I need the output as below as another dataframe
StudentID Grades
101 A,B,C
102 B,C
How to do this in pandas. When i groupby, it returns a groupby object and not a dataframe
Upvotes: 0
Views: 233
Reputation: 97331
import pandas as pd
df = pd.DataFrame({
'StudentID': [101,101,101,102,102],
'Grades': ['A','C','B','B','C']
})
df.groupby('StudentID').Grades.agg(",".join)
Upvotes: 2
Reputation: 14748
You can access the DataFrame
you want via the returned groupby
object:
import pandas as pd
df = pd.DataFrame({
'StudentID': [101,101,101,102,102],
'Grades': ['A','C','B','B','C']
})
grouped = df.groupby('StudentID')
for student_id, df_student in grouped:
print 'ID:', student_id
print df_student
Output:
ID: 101
Grades StudentID
0 A 101
1 C 101
2 B 101
ID: 102
Grades StudentID
3 B 102
4 C 102
You can also access the DataFrame
with a specific key
print grouped.get_group(101)
which will return the DataFrame
with all StudentID
being equal to 101.
Upvotes: 0