我正在使用的数据帧有4个可能的组合,超过2列和几百个组。在
| Group | Before | After |
|:-----:|:----------:|:----------:|
| G1 | Injection | Injection |
| G1 | Injection | Production |
| G1 | Production | Injection |
| G1 | Production | Production |
有3个预计算的柱需要根据下面所示的前/后组合进行拉伸。在
^{pr2}$我试过多个嵌套np.哪里的
np.where(df['Before'] == 'Injection' & df['After'] == 'Injection', df['DTI'],
np.where(....))
结果是:
ValueError: either both or neither of x and y should be given
和嵌套多个np.逻辑公司名称:
np.where(np.logical_and(df['Before'] == 'Injection' & df['After'] == 'Injection'), df['DTP'])
结果是:
the truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我已经到了我能做的事情的上限,需要一些想法!在
Before["Injection"]
没有按照您的想法操作。在你展示的代码中,它甚至没有被定义。在你可能想要的是:
如果您有许多这样的组合,那么使用另一个答案中建议的
apply
可能更合适。在如果您有很多行,那么将布尔索引(例如
^{pr2}$df["Before"] == "Production"
)保存到变量中,然后直接保存如果您也只有这两种状态,您可以通过使用一元否定运算符
~
免费获得第二种状态:一种方法是使用apply函数:
假设您的数据帧在变量
df
中,您可以执行以下操作:相关问题 更多 >
编程相关推荐