Home > Back-end >  I want to draw a line chart with a sort of condition
I want to draw a line chart with a sort of condition

Time:10-31

I have a dataframe containing 1440 rows and 8 columns. I have a column names 'number' taking values from 0 to 12. And I have another column named "correct" which takes value of True or False.

What I would like to do is a line chart with on my x axis'number' which as I said are ranging from 0 to 12 and on my y axis the number of "correct" taking the value True.

What I want is to regroup all of the cases when the 'number' is 0 and number of true, for example.

I tried to create a small example but I think I'm not precise enough.

I tried a lot of things with grouby and other things

CodePudding user response:

I used the where and the groupby functions. So basically I filtered rows where the correct column was True, and then grouped by the numbers. You can just plot this new dataframe. I used a random dataframe to test the result.

Edit: I realized that I only used numbers 0-10 instead of 0-12, but the idea is still the same.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

number = np.random.randint(0, 10, size=1440)
correct = np.random.choice([False, True], size=1440)

df = pd.DataFrame(data = zip(number, correct),
                  columns = ['number', 'correct'])

sums = df.where(df['correct'] == True).groupby(['number']).count()

plt.plot(sums)
plt.show()

enter image description here

CodePudding user response:

Thanks a lot, I have been able to draw what I wanted but just for my graph to be a bit more precise. How could I add my axis like on x I would like 'number' and on the top left of my graph something saying what's being presented like the name 'correct' because I'm plotting the number of 'correct' ?

  • Related