Pandas:如何在groupby和unstack之后删除索引列?

Jon*_*nas 3 python dataframe pandas

我在 groupby 和 unstack a DataFrame 之后删除 pandas 中的索引列时遇到问题。

我原来的 DataFrame 看起来像这样:

example = pd.DataFrame({'date': ['2016-12', '2016-12', '2017-01', '2017-01', '2017-02', '2017-02', '2017-02'], 'customer': [123, 456, 123, 456, 123, 456, 456], 'sales': [10.5, 25.2, 6.8, 23.4, 29.5, 23.5, 10.4]})
example.head(10)
Run Code Online (Sandbox Code Playgroud)

输出:

日期 顾客 销售量
0 2016年12月 123 10.5
1 2016年12月 第456章 25.2
2 2017年01月 123 6.8
3 2017年01月 第456章 23.4
4 2017年2月 123 29.5
5 2017年2月 第456章 23.5
6 2017年2月 第456章 10.4

请注意,一名客户每月可能有多次销售(如第 5 行和第 6 行)。

我的目标是将 DataFrame 转换为聚合 DataFrame,如下所示:

顾客 2016年12月 2017年01月 2017年02月
123 10.5 6.8 29.5
234 25.2 23.4 33.9

到目前为止我的解决方案:

example = example[['date', 'customer', 'sales']].groupby(['date', 'customer']).sum().unstack('date')
example.head(10)
Run Code Online (Sandbox Code Playgroud)

输出:

销售量
日期 2016年12月 2017年01月 2017年02月
顾客
123 10.5 6.8 29.5
234 25.2 23.4 33.9
example = example['sales'].reset_index(level=[0])
example.head(10)
Run Code Online (Sandbox Code Playgroud)

输出:

日期 顾客 2016年12月 2017年01月 2017年02月
0 123 10.5 6.8 29.5
1 234 25.2 23.4 33.9



此时我无法删除“日期”列:

example.reset_index(drop = True)
example.head()
Run Code Online (Sandbox Code Playgroud)

输出:

日期 顾客 2016年12月 2017年01月 2017年02月
0 123 10.5 6.8 29.5
1 234 25.2 23.4 33.9

它只是保持不变。你有什么想法吗?

sam*_*mmy 6

您的解决方案的替代方案,但关键是添加 a rename_axis(columns = None),因为它date是列轴的名称:

(example[["date", "customer", "sales"]]
.groupby(["date", "customer"])
.sum()
.unstack("date")
.droplevel(0, axis="columns")
.rename_axis(columns=None)
.reset_index())

    customer    2016-12 2017-01 2017-02
0   123          10.5   6.8     29.5
1   456          25.2   23.4    33.9
Run Code Online (Sandbox Code Playgroud)