为什么数据框变成了一个系列?

Why does the dataframe becomes a series?

为什么下面的函数groupby().size()会把dataframe变成一个series?在第一行中,我得到了 info() 函数的结果,但在第三行中,我收到了一个错误。 'Series' 对象没有属性 'info'

print(df_injury_record.info())
test = df_injury_record.groupby(["Surface"]).size()
print(test.info())

当我想将它放在数据框中时,该函数是什么样子的?

问候

它 return 是一个 series,因为每个组只有一个功能(因此它不会 return 您的所有列)。文档说:

pandas.core.groupby.GroupBy.size

Compute group sizes.

Returns: Series, number of rows in each group.

如果您真的想要 dataframe,只需在末尾添加 .to_frame()