这对熊猫来说很容易做到。您可以在他们的文档中查看有关语法的进一步解释,但代码如下:
import pandas as pd
data_list = [
["John", "Physics", 5], ["John", "PC", 7], ["John", "Math", 8],
["Mary", "Physics", 6], ["Mary", "PC", 10], ["Mary", "Algebra", 7],
["Helen", "Physics", 7], ["Helen","PC", 6], ["Helen", "Algebra", 8],
["Helen", "Analysis", 10], ["Bill", "PC", 10], ["Bill", "Analysis", 6],
["Bill", "Math", 8], ["Bill", "Biology", 6], ["Michael", "Analysis", 10]
]
# Convert the data_list array into a DataFrame
df = pd.DataFrame(data_list, columns=['Name', 'Subject', 'Grade'])
# Group by Subject extracting the count and mean of the accumulated grade per subject
df.groupby('Subject').Grade.agg(['count', 'mean'])