Trouble creating a data boxplot

49 Views Asked by At

I'm trying to extract variables from a data spreadsheet to form a boxplot which looks like this:

 import matplotlib.pyplot as plt
 import pandas as pd

 pulserate = pd.read_csv("PulseNew.csv")
 ran_yes = pulserate[pulserate["Ran"] == "yes"]
 ran_no = pulserate[pulserate["Ran"] == "no"]
 ry = ran_yes
 rn = ran_no
 ran_data = [ry, rn]
 plt.boxplot(ran_data)
 plt.show()

However, when I do this I end up receiving this error:

ValueError: X must have 2 or fewer dimensions

Upon seeing that, I tried inserting these lines instead:

pulserate = pd.read_csv("PulseNew.csv")
ran_yes = pulserate[pulserate["Ran"] == "yes"]
ran_no = pulserate[pulserate["Ran"] == "no"]
subset = pulserate[(pulserate["Ran"] ==  "yes") & (pulserate["Ran"] == "no")]
plt.boxplot(subset)
plt.show()

While this did not result in an error, all I got was an empty graph. For context, I'm a beginning Python programmer.

1

There are 1 best solutions below

3
Elnaz On

You must extract the column that you want to plot from each data frame and pass it as a 1D array:

pulserate = pd.read_csv("PulseNew.csv")
ran_yes = pulserate[pulserate["Ran"] == "yes"]
ran_no = pulserate[pulserate["Ran"] == "no"]
ry = ran_yes["Pulse"] # extract the Pulse column from ran_yes
rn = ran_no["Pulse"] # extract the Pulse column from ran_no
ran_data = [ry, rn] # create a list of 1D arrays
plt.boxplot(ran_data) 
plt.show() 

Try this way and give me the result please.