I have a df which is unsorted and has around 750,000 rows. Now I want to group the df by mrwSmpVWi
and get the average of my mrwSmpP
. For example: I have 2,000 mrwSmpP
values for mrwSmpVWi = 3
I want to get the average of the 2,000 values. If I have one y value for every x value I want to plot it by another group which is the column 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:
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. 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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.