pandas groupby plot values

2024/10/11 18:21:58

I have a pandas dataframe that looks like this:

      **real    I     SI     weights**0       1     3      0.3  0       2     4      0.20       1     3      0.50       1     5      0.51       2     5      0.3  1       2     4      0.21       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. At the end, I should have, for each realization, something like that:

    real = 0:I = 1     SI = 3      weight = 0.8SI = 5      weight = 0.5I = 2     SI = 4      weight = 0.2real = 1:  I = 1     SI = 3      weight = 0.5I = 2     SI = 5      weight = 0.3SI = 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).

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...

Answer

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

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

subplots over real, I values

https://en.xdnf.cn/q/118293.html

Related Q&A

Any python module for customized BNF parser?

friends.I have a make-like style file needed to be parsed. The grammar is something like:samtools=/path/to/samtools picard=/path/to/picardtask1: des: descriptionpath: /path/to/task1para: [$global.samto…

How to draw an histogram with multiple categories in python

I am a freshman in python, and I have a problem of how to draw a histogram in python.First of all, I have ten intervals that are divided evenly according to the length of flowers petal, from min to max…

Turtle in Tkinter creating multiple windows

I am attempting to create a quick turtle display using Tkinter, but some odd things are happening.First two turtle windows are being created, (one blank, one with the turtles), secondly, any attempt of…

Array tkinter Entry to Label

Hey Guys I am beginner and working on Project Linear and Binary search GUI application using Tkinter, I want to add multiple Entry boxes values to label and in an array here, I tried but its not workin…

Grid search and cross validation SVM

i am implementing svm using best parameter of grid search on 10fold cross validation and i need to understand prediction results why are different i got two accuracy results testing on training set not…

Accessing dynamically created tkinter widgets

I am trying to make a GUI where the quantity of tkinter entries is decided by the user.My Code:from tkinter import*root = Tk()def createEntries(quantity):for num in range(quantity):usrInput = Entry(roo…

Graphene-Django Filenaming Conventions

Im rebuilding a former Django REST API project as a GraphQL one. I now have queries & mutations working properly.Most of my learning came from looking at existing Graphene-Django & Graphene-Py…

Summing up CSV power plant data by technology and plant name

Ive got a question regarding the Form 860 data about US power plants.It is organized block-wise and not plant-wise. To become useful, the capacity numbers must be summed up.How may I get the total capa…

Send and receive signals from another class pyqt

I am needing a way to receive signals sent by a Class to another class. I have 2 classes: In my first class I have a function that emits a signal called asignal In my second class I call the first cla…

I can not add many values in one function

I have a gui applicationI put text into text box1, text box2,………… text box70 ,and then click on the pushButton, The function return_text () in the module_b.py be called. Now I can call one instance…