【发布时间】:2021-12-08 11:22:43
【问题描述】:
我有一个如下所示的数据框:
feature target
0 2 0
1 0 0
2 0 0
3 0 0
4 1 0
... ... ...
33208 1 0
33209 0 0
33210 2 0
33211 2 0
33212 1 0
feature 列中有 3 个类(0、1、2),target 列中有两个类(0、1)。如果我按这两列对数据框进行分组,我会得到:
df.groupby(['feature', 'target']).size()
feature target
0 0 4282
1 81
1 0 8537
1 37
2 0 20161
1 115
dtype: int64
每个feature 类都有0s 和1s 作为target 值,我需要找到一种对这些值进行采样的方法,我的意图是最后有这样的东西:
new_df.groupby(['feature', 'target']).size()
feature target
0 0 4282
1 81
1 0 4282
1 37
2 0 4282
1 115
dtype: int64
我需要对每个feature 类的target 值进行抽样,有什么建议吗?
【问题讨论】:
-
这不是已经采样了吗?
标签: python pandas pandas-groupby