Home > front end >  Barplot for a range of numbers appearing in column - python
Barplot for a range of numbers appearing in column - python

Time:09-17

I have a column where values are either NOT_TESTED, NOT_COMPLETED, TOO_LOW, or a value between 150 and 190, seperated by 5, so 150, 155, 160etc).

num_range = range(150,190, 5)
OUTCOMES = ['NOT_TESTED', 'NOT_COMPLETED', 'TOO_LOW', num_range]
df["outcomes_col"] = df["TP1"].astype ("category")
df["outcomes_col"].cat.set_categories(OUTCOMES , inplace = True )
sns.countplot(x= "outcomes_col", data=df, palette='Magma')
plt.xticks(rotation = 90)
plt.ylabel('Amount')
plt.xlabel('Outcomes')
plt.title("Outcomes per testing")
plt.show()  

This is the code I created, it works fine except when I need to get the numerical values between 150&190. I have tried different ways like xrange() or tuple(range(150,190, 5) for num_range but none of these work. I need each value (150, 155 etc) to have its own stick in the barplot that represents how many time it appears in that column "TP1"

Any guidance?

Using python2.7

CodePudding user response:

You can do as below:

num_range = range(150,190, 5)
OUTCOMES = ['NOT_TESTED', 'NOT_COMPLETED', 'TOO_LOW']
OUTCOMES.extend(list(num_range))


Output:
['NOT_TESTED',
 'NOT_COMPLETED',
 'TOO_LOW',
 150,
 155,
 160,
 165,
 170,
 175,
 180,
 185]

Maybe then you can categorize and continue with the rest.

CodePudding user response:

Try this:

num_range = list(range(150,190, 5)) #as list

OUTCOMES = ['NOT_TESTED', 'NOT_COMPLETED', 'TOO_LOW']
OUTCOMES.extend(num_range) #extend it with the list of numbers

#add numbers to the TP1 column
df = df.append(pd.DataFrame(num_range, 
               columns=['TP1']),
               ignore_index = True)

df["outcomes_col"] = df["TP1"].astype("category")
df["outcomes_col"].cat.set_categories(OUTCOMES , inplace = True )
sns.countplot(x= "outcomes_col", data=df, palette='Magma')
plt.xticks(rotation = 90)
plt.ylabel('Amount')
plt.xlabel('Outcomes')
plt.title("Outcomes per testing")
plt.show()  

enter image description here

  • Related