【发布时间】:2023-02-01 00:44:27
【问题描述】:
我问了一个非常相似的问题here,不幸的是,玩具问题对我来说太简单了。
我有一个数据框,其中包含两种不同条件下的主题和各种渠道的许多值列。
d = {
"subject": [1, 1, 2, 2, 3, 3],
"condition": ["on", "off", "on", "off", "on", "off"],
"channel": [1, 1, 1, 1, 1, 2]
"value": [1, 2, 3, 5, 4, 6]
}
df = pd.DataFrame(data=d)
df
| subject | condition | channel | value | |
|---|---|---|---|---|
| 0 | 1 | on | 1 | 1 |
| 1 | 1 | off | 1 | 2 |
| 2 | 2 | on | 1 | 3 |
| 3 | 2 | off | 1 | 6 |
| 4 | 3 | on | 1 | 4 |
| 5 | 3 | off | 2 | 6 |
我想获得表明差异的新列关闭在两种情况之间对于每个频道.如果在一种情况下缺少一个频道,我想得到 nan。在这种情况下,我想得到:
| subject | condition | channel | value | value_off-on | |
|---|---|---|---|---|---|
| 0 | 1 | on | 1 | 1 | 1 |
| 1 | 1 | off | 1 | 2 | 1 |
| 2 | 2 | on | 1 | 3 | 3 |
| 3 | 2 | off | 1 | 6 | 3 |
| 4 | 3 | on | 1 | 4 | nan |
| 5 | 3 | off | 2 | 6 | nan |
我最好怎么做?
我必须尝试扩展解决方案here,但是,这会引发错误:
df['off-on'] = df.set_index(['subject', 'channel']).map(
df.pivot(index=['subject', 'channel'], columns='condition', values='value')
.eval('off-on')
)
AttributeError: 'DataFrame' object has no attribute 'map'
s = df.set_index(['condition', 'subject', 'channel'])['value']
df['off-on'] = df[['subject', 'channel']].map(s['off']-s['on'])
AttributeError: 'DataFrame' object has no attribute 'map'
解决办法是什么?
【问题讨论】:
-
每个主题可以有多个开关吗?