简体   繁体   English

pandas groupby 和 select 来自另一列的 ecah 组的值,用于绘图

[英]pandas groupby and select value from aother column for ecah group for plotting

I am using groupby() and for plotting each group.我正在使用groupby()并绘制每个组。 I have a dataframe: which each group have the same 'normal' value我有一个 dataframe:每个组都有相同的“正常”值

id  date  value  normal
1    5.2  20      200   
1    5.4  100     200
1    6.9   30     200
2    2.4   20     500
2    3.4   100    500

I want to plot each group date and value columns (for x,y axes) and add axhline with the normal values correspond to the group.我想 plot 每个组日期和值列(对于 x,y 轴)并添加与组对应的正常值的 axhline。 I've tried:我试过了:

ax = sns.scatterplot("date", "value", data = data)
grouped = data.groupby('id')
normal_val= grouped['normal']
plt.axhline(normal_val,c='red', ls=':')    #===> THIS LINE
plt.show()

but does not work.但不起作用。 Hope someone could help!希望有人能帮忙! thanks谢谢

Referring to the highlighted line, the normal_val has more than 1 value, not sure which one are you interested in.参考突出显示的行, normal_val有超过 1 个值,不确定您对哪一个感兴趣。

print(data.groupby('id')[['normal']].mean())

     normal
id         
1.0   200.0
2.0   500.0

If you change the line to something like below, you will get an output如果将行更改为如下所示,您将获得 output

normal_val1 = 200
normal_val2 = 500
plt.axhline(normal_val1, c='red', ls=':')
plt.axhline(normal_val2, c='green', ls='-.')

在此处输入图像描述

Edit: Depending on how many 'normal' values you have, you can add to the color list c .编辑:根据您有多少“正常”值,您可以添加到颜色列表c I've started with 8, so this code should work:我从 8 开始,所以这段代码应该可以工作:

import seaborn as sns
import matplotlib.pyplot as plt

ax = sns.scatterplot("date", "value", data = data)
df = data.groupby('id')[['normal']].mean().reset_index()
c = ['red', 'green', 'yellow', 'blue', 'navy', 'cyan', 'pink', 'brown']
if len(df['normal']) <= len(c):
    for i, normal in enumerate(df['normal']):
        plt.axhline(normal, c=c[i], ls='-.')
plt.show()

This is because the line grouped = data.groupby('id) return pandas.core.groupby.generic.DataFrameGroupBy object .这是因为行grouped = data.groupby('id) return pandas.core.groupby.generic.DataFrameGroupBy object

You might want to try -您可能想尝试 -

data = {
    "id": [1, 1, 1, 2, 2],
    "date":  [5.2, 5.4, 6.9, 2.4, 3.4],
    "value": [20, 100, 30, 20, 100],
    "normal": [200, 200, 200, 500, 500]
}

data = pd.DataFrame(data)

for id in data["id"].unique():
    plot_data = data.loc[data["id"] == id]
    ax = sns.scatterplot("date", "value", data = plot_data)
    ax.axhline(plot_data["normal"].unique(), c='red', ls=':')
    plt.show()

Note that df.groupby('id').normal itself doesn't make sense.请注意, df.groupby('id').normal本身没有意义。 You should apply an aggregating function to it afterward.之后您应该对其应用聚合 function 。 Otherwise, it is just a groupby object.否则,它只是一个由 object 组成的组。 I take the mean of normal values for each id, but of course you can play with it.我取每个 id 的正常值的平均值,但当然你可以使用它。

normal_val = df.groupby('id').normal.mean()
for item in normal_val:
    plt.axhline(y=item, c='red', ls=':')
plt.show()

gives

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM