我有一个类似于这样的pandas数据框:
| Cliid | Segment | Insert |
|-------|---------|--------|
| 001 | A | 0 |
| 002 | A | 0 |
| 003 | C | 0 |
| 004 | B | 1 |
| 005 | A | 0 |
| 006 | B | 0 |
我希望将其分为2组,使每个变量在 [Segment, Insert] 中的构成在两组中相同。例如,每个组将有属于 Segment A 的观测值的1/2,Insert = 1 的1/6等等。
我已经查看了this answer,但它只适用于一个变量的分层,无法对多个变量进行操作。
R有this function可以做到这一点,但不能使用R。
顺便说一下,我正在使用Python 3。
stratify
在多列上似乎无法工作。当我运行您的代码时,我得到ValueError: Found input variables with inconsistent numbers of samples: [6, 1]
。如果我删除stratify
,它可以工作。 - arthurstratify=[["Segment", "Insert"]]
改成了stratify=df[["Segment", "Insert"]]
。 - Jannik