Python DataFrameGroupBy对象到DataFrame
我想将我拥有的Python DataFrameGroupBy对象到DataFrame,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我想将我拥有的pandas.DataFrame中的值分组,然后将结果存储在另一个数据帧中 我有以下代码: grouped = df.groupby("date") grouped_df = DataFrame() grouped_df["work_count"] = grouped["id"].count() grouped_df["author_author"] = grouped["author_name"].nunique() print(grouped_df) 有了它,我得到:
pandas.DataFrame
中的值分组,然后将结果存储在另一个数据帧中
我有以下代码:
grouped = df.groupby("date")
grouped_df = DataFrame()
grouped_df["work_count"] = grouped["id"].count()
grouped_df["author_author"] = grouped["author_name"].nunique()
print(grouped_df)
有了它,我得到:
commit_count authors_count
date
2018-02-16 1 1
2018-02-23 2 1
2018-03-02 5 1
2018-03-30 1 1
2018-04-13 3 1
2018-06-15 2 1
我尝试使用索引
获取日期,如下所示:
grouped = df.groupby("date")
grouped_df = DataFrame()
grouped_df["date"] = grouped.indices
grouped_df["work_count"] = grouped["id"].count()
grouped_df["author_author"] = grouped["author_name"].nunique()
print(grouped_df)
有了它,我陷入了这样的困境:
date work_count author_count
2018-02-16 2018-02-16 1 1
2018-02-23 2018-02-23 2 1
2018-03-02 2018-03-02 5 1
2018-03-30 2018-03-30 1 1
2018-04-13 2018-04-13 3 1
2018-06-15 2018-06-15 2 1
我也尝试过:
grouped = df.groupby("date", as_index=False)
但我得到:
ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series
此外,我希望将日期保留为数据框中的一列,而使用as_index=False
无法实现这一点
最后,我想从grouped\u df
中删除第一列。
我希望我的结果如下所示:
date work_count author_count
2018-02-16 1 1
2018-02-23 2 1
2018-03-02 5 1
2018-03-30 1 1
2018-04-13 3 1
2018-06-15 2 1
grouped=df.groupby(“date”,as_index=False)。reset_index()你的最终结果和起点有什么区别?他们看起来identical@BrianJoseph由于我的起点日期不是一列(请注意,“日期”是其他列名下面的一行),所以这些是索引而不是值。如果我尝试使用分组的_df[“date”],我会得到一个键错误。在我的最终结果中,“date”是一列,日期是该列中的值。使用此选项,我将丢失“date”,并希望保留它。