简体   繁体   中英

How to groupby a pandas dataframe

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM