[英]pandas groupby plot values
I have a pandas dataframe that looks like this: 我有一个看起来像这样的熊猫数据框:
**real I SI weights**
0 1 3 0.3
0 2 4 0.2
0 1 3 0.5
0 1 5 0.5
1 2 5 0.3
1 2 4 0.2
1 1 3 0.5
I need to divide it by "real", then I need to do the following: 我需要将其除以“真实”,然后需要执行以下操作:
given a value of I, consider each value of SI and add the total weight. 给定I值,请考虑SI的每个值,然后加上总重量。 At the end, I should have, for each realization, something like that:
最后,对于每个实现,我都应该有这样的东西:
real = 0:
I = 1 SI = 3 weight = 0.8
SI = 5 weight = 0.5
I = 2 SI = 4 weight = 0.2
real = 1:
I = 1 SI = 3 weight = 0.5
I = 2 SI = 5 weight = 0.3
SI = 4 weight = 0.2
The idea is then to plot, for each value of I and real, on the x axis the values of SI and on the y axis the relative total weight (normalized to 1). 然后的想法是,对于I和real的每个值,在x轴上绘制SI的值,并在y轴上绘制相对总重量(标准化为1)。
What I tried to do was this: 我试图做的是:
name = ['I', 'SI','weight', 'real']
Location = 'Simulationsdata/prova.csv'
df = pd.read_csv(Location, names = name,sep='\t',encoding='latin1')
results = df.groupby(['I', 'real', 'SI']).weight.sum()
When I print results, I have the table I want, but now I don`t know how to make a plot as I wanted, because I do not know how to get the SI values... 当我打印结果时,我有了想要的表,但是现在我不知道如何绘制想要的图,因为我不知道如何获取SI值...
Once you do this: 完成此操作后:
results = df.groupby(['real', 'I', 'SI'])['weights'].sum()
You can get the values of 'real'
, 'I'
and 'SI'
stored in the dataframe by using 您可以使用以下方法获取存储在数据框中的
'real'
, 'I'
和'SI'
results.index.get_level_values(0)
Int64Index([0, 0, 0, 1, 1, 1], dtype='int64', name='real'
results.index.get_level_values(1)
Int64Index([1, 1, 2, 1, 2, 2], dtype='int64', name=' I')
results.index.get_level_values(2)
Int64Index([3, 5, 4, 3, 4, 5], dtype='int64', name=' SI')
You can iterate over those to get the plots you want. 您可以遍历这些图以获得所需的图。 For example:
例如:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)
for idx1, i in enumerate(results.index.get_level_values(0).unique()):
for idx2, j in enumerate(results.index.get_level_values(1).unique()):
axes[idx1, idx2].plot(results.loc[i, j], 'o')
axes[idx1, idx2].set_xlabel('SI')
axes[idx1, idx2].set_ylabel('weights')
axes[idx1, idx2].set_xlim([0, 6])
axes[idx1, idx2].set_ylim([0, 1])
axes[idx1, idx2].set_title('real: {} I: {}'.format(i, j))
plt.tight_layout()
plt.show()
which gives 这使
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.