小编LIB*_*LIB的帖子

pandas 中的 groupby 并从输出 DataFrame 中排除 grouper 列

我正在尝试对 Pandas df 进行分组,以便将键作为索引保留,但它不包含每个组中的键。

这是我的意思的一个例子。

  1. 原始数据框

    ungrouped_df = pd.DataFrame({'col1':['A','A','B','C','C','C'], 'col2':[8,5,1,4,1,2], 'col3':[7,4,2,1,2,1],'col4':[1,8,0,2,0,0]})

出去:

| index | col1 | col2 | col3 | col4 |
|-------|------|------|------|------|
| 1     |    A |    8 |    7 |    1 |
| 2     |    A |    5 |    4 |    8 |
| 3     |    B |    1 |    2 |    0 |
| 4     |    C |    4 |    1 |    2 |
| 5     |    C |    1 |    2 |    0 |
| …
Run Code Online (Sandbox Code Playgroud)

python pandas pandas-groupby

4
推荐指数
1
解决办法
165
查看次数

标签 统计

pandas ×1

pandas-groupby ×1

python ×1