[英]Groupby Pandas dataframe and plot
I have a Pandas dataframe and wish to plot from a specific column by group. 我有一个Pandas数据框,希望按组从特定列进行绘制。 The goal is to have a separate graph for each org (like the image below). 目标是为每个组织有一个单独的图 (如下图所示)。
What is the most efficient way to accomplish this? 最有效的方法是什么?
Source excerpt: 资料摘录:
date time1 time2 time3 org
2017-11-16 02:14:20 36.46 20.42 16.04 abc
2017-11-21 08:39:15 40.12 10.18 29.94 abc
2017-11-06 08:21:08 33.00 0.00 33.00 bcd
2017-10-30 14:29:03 47.50 10.33 37.17 bcd
2017-11-09 07:51:48 31.92 1.92 30.00 efg
2017-11-07 08:53:05 33.00 1.04 31.96 efg
Attempted Code: 尝试输入的代码:
# load data
df = pd.read_csv('Dataset.csv')
# identify unique values in org col
org_group = sorted(df['org'].unique())
# set date as index for plot
df = df.set_index('date')
df.head()
for i in range(len(org_group)):
# select specific org value from df
df_group = df[df.org == org_group[i]]
# display plot
df_plot = df_group.resample('W').agg('median').plot(title="Median Times for "+org_group[i])
plt.show()
# iterate
i = i+1
If your dataframe is named df
如果您的数据框名为df
df = df[df.org == 'abc']
will filter it for abc
将其过滤为abc
To get a list of unique items in org
column use df.org.unique().tolist()
要获得org
列中唯一项的列表,请使用df.org.unique().tolist()
Then you can iterate through the list and get a separate dataframe for each of the orgs 然后,您可以遍历列表并为每个组织获取单独的数据框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.