[英]Plot average of y values for every x value
I have a df which is unsorted and has around 750,000 rows.我有一个未排序的 df,大约有 750,000 行。 Now I want to group the df by
mrwSmpVWi
and get the average of my mrwSmpP
.现在我想通过
mrwSmpVWi
对 df 进行mrwSmpVWi
并获得我的mrwSmpP
的平均值。 For example: I have 2,000 mrwSmpP
values for mrwSmpVWi = 3
I want to get the average of the 2,000 values.例如:对于
mrwSmpVWi = 3
我有 2,000 个mrwSmpP
值,我想获得 2,000 个值的平均值。 If I have one y value for every x value I want to plot it by another group which is the column Seriennummer
.如果每个 x 值都有一个 y 值,我想通过另一组绘制它,即列
Seriennummer
。
for number in df.groupby('mrwSmpVWi'):
df['m'] = df['mrwSmpP'].mean()
fig, ax = plt.subplots(figsize=(30,15))
for name, group in df.groupby('Seriennummer'):
group.plot(x="mrwSmpVWi", y="m", ax=ax, marker='o', linestyle='', ms=12, label =name)
plt.show()
If I do it like this.如果我这样做。 I will just get a straight line for my average values.
我只会得到我的平均值的直线。
This is a part of my df:这是我的 df 的一部分:
EDIT:编辑:
I changed it this way:我是这样改的:
#for number in df.groupby('mrwSmpVWi'):
df['m'] = df.groupby('mrwSmpVWi')['mrwSmpP'].mean()
It doesn't make a change if I do it with or without the for loop.如果我使用或不使用 for 循环,它都不会改变。 I will get this diagram:
我会得到这个图:
It seems you are overwritting the mean column on each iteration of the first loop.您似乎在第一个循环的每次迭代中覆盖了均值列。 Also, to save a mean on each value of the dataframe, you need to use a merge.
此外,要保存数据帧的每个值的平均值,您需要使用合并。 Try:
尝试:
df = df.merge(df.groupby('mrwSmpVWi')["mrwSmpP"].mean().rename("m").reset_index(), on="mrwSmpVWi", how="left")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.