gpt4 book ai didi

python - 为 pandas 数据框的分组列绘制一列差异图

转载 作者:太空宇宙 更新时间:2023-11-04 04:07:44 25 4
gpt4 key购买 nike

我有一个数据框,下面是一个简化的例子:

   cycle  sensor  value
0 0 1 0.34
1 0 1 0.80
2 0 2 0.12
3 0 2 0.62
4 1 1 0.01
5 1 1 0.75
6 1 2 0.06
7 1 2 0.02

我想为每个传感器绘制“值”列的差异,对于每个周期:将 x 轴作为周期编号,y 轴作为值的差异,每个系列都是给定的传感器编号.例如传感器一的线在周期 0 和 1 上为 0.46 和 0.74。

实际上,我有更多的列(我的这部分代码没有使用)并且有 144 个循环和 37 个循环。每个传感器每个周期有几千个值。

这是我写的代码,我没有得到错误:创建了一个图形但没有数据显示。

groups = unstacked_data.groupby(["cycle", "pressure"])

fig,ax = plt.subplots()
ax.set_xlabel("Cycle Number")
ax.set_ylabel("Change in Normalised Pressure")


for cycle, group in groups:
ax.plot(cycle[0],group.value.max()-group.value.min(), label=group.pressure)

我不确定我做错了什么,任何建议将不胜感激! :)

最佳答案

好的,我想我现在明白了。结果应该是这样的吗?

diff = df.groupby(["sensor","cycle"]).apply(lambda x:x.value.max()-x.value.min()).unstack()

#Output: each value in the table is max-min. Columns are cycles and rows are sensors
cycle 0 1
sensor
1 0.46 0.74
2 0.50 0.04

现在您可以按行绘制它,即按传感器。

关于python - 为 pandas 数据框的分组列绘制一列差异图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56931413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com