1

enter image description here

The row consist of around 6000 line items. I am trying to display the bar chart based on a certain condition. I am trying to categorize the Application data using the [Severity,Immediate_Action,Response_code] this will provide me the important data which I visualize it. When the condition is matched. I would like to plot the Application column in a form of bar chart either in x or y-axis.

So far I have to set a condition but I am not sure how will get the value_counts for each Application based on the below condition.

High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None')]

I am not good with pandas still trying to learn, but I wonder if I have to use groupby.

import pandas as pd
report = pd.read_csv('Test-Report.csv')
High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None')]
report.groupby['Application']

Not sure how to call the condition in the groupby or if there is any other method to do it. To draw a bar chart with respect to each Application. The given set of applications in the csv are only 7. I am not sure if the value_counts() will work in this.

bI_jOdev
  • 47
  • 1
  • 9
  • 1
    Could you provide a [sample dataframe](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)? – m13op22 Aug 21 '19 at 18:15
  • Sample data would go a long way here. You are possibly looking into seaborn barplot (for instance), with hue and multiple categories. – realr Aug 21 '19 at 21:28
  • @HS-nebula I hope this works. couldn't paste the csv data. – bI_jOdev Aug 22 '19 at 06:03
  • @HS-nebula Anyway to join both the conditions. connect both these condition together. ```High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None')].report.groupby('Application')['Num._of_Events'].sum().sort_values(ascending=False)``` – bI_jOdev Aug 22 '19 at 10:00
  • Are you trying to get the counts for the entire dataset (`report`) or only those that meet the condition (`High_Alert`)? – m13op22 Aug 22 '19 at 13:56
  • Also, please don't post images of your dataset. Follow the instructions in the Complete section in this [link](https://stackoverflow.com/help/minimal-reproducible-example). – m13op22 Aug 22 '19 at 13:57
  • @HS-nebula I am trying to get the sum of Num_of_events for each applications based on this ```High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None')] ``` – bI_jOdev Aug 22 '19 at 13:58
  • Then just do `High_Alert.groupby('Application')['Num. of Events'].sum()` – m13op22 Aug 22 '19 at 14:05
  • @HS-nebula This is the code which I am trying but no luck `import seaborn as sns from matplotlib import pyplot as plt High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None') App_detail = High_Alert.groupby((report('Application')['Num._of_Events'])).sum() plt.figure(figsize=(8,4)) sns.set(style='darkgrid') ax = sns.countplot(x='Application',data=App_detail,order=report['Application'].value_counts(ascending=True).index) ax.set_xticklabels(ax.get_xticklabels(),rotation=40,ha='right') plt.show()` – bI_jOdev Aug 22 '19 at 14:14
  • Okay, you're going to have to open a separate question for that then, this is getting too long. – m13op22 Aug 22 '19 at 14:21

2 Answers2

2

You can use seaborn library

import pandas as pd

import seaborn as sns

report = pd.read_csv('Test-Report.csv') 

High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None')]

report = pd.read_csv('Test-Report.csv')

sns.countplot('Application',data=High_Alert) #here you can also use hue like hue = 'Severity'

m13op22
  • 2,168
  • 2
  • 16
  • 35
Aniket Dixit
  • 152
  • 1
  • 5
  • thank you so much for this. It displays as expected but the application names are not clear it overwrites other. One more thing, if you see the Num_of_Events column if I sum it up with respect to each application with the help of value_counts, can I call this in sns.countplot. – bI_jOdev Aug 22 '19 at 06:27
  • If Application name's are overwriting other's then...you have to run this. plt.figure(figsize=(10,8)) ///means you have to set figure size – Aniket Dixit Aug 22 '19 at 06:34
  • The other thing which I tried was set_xticklabels(ax.get_xticklabels(),rotation=40,ha='right'), it worked well. In the same countplot, will I be able to fill different color each application both for High and Medium Severity. – bI_jOdev Aug 22 '19 at 08:06
  • yes,you can you have to store that bar plot into a variable.After that you have to apply a loop on that variable.then you can easily set colors.for each bar!! – Aniket Dixit Aug 22 '19 at 08:28
  • Okay. the other part which I was asking about the Num_of_Events per each 'Application'. I was able to get it, but how will I join both the conditions together.```report.groupby('Application')['Num._of_Events'].sum().sort_values(ascending=False) ``` – bI_jOdev Aug 22 '19 at 08:43
  • Anyone please how will I connect both these condition together. ```High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None')].report.groupby('Application')['Num._of_Events'].sum().sort_values(ascending=False)``` – bI_jOdev Aug 22 '19 at 09:27
0

For anyone who need help, it worked as expected. But still I was not able to plot the bar chart as expected. but the code is perfect for filtering purpose.

import seaborn as sns
from matplotlib import pyplot as plt
High_Alert = report[(report['Response_Code'] == 200) & (report['Severity'].isin(['High','Medium'])) & (report['Immediate_Action']== 'None')].groupby('Application')['Num._of_Events'].sum().sort_values()
ax = High_Alert.plot(x='list of applications', y='Num. of Events', kind='bar',figsize=(6,6))
ax.set_xlabel('List of Appliations')
ax.set_ylabel('Num of Events')
ax.set_xticklabels(ax.get_xticklabels(),rotation=40,ha='right')
bI_jOdev
  • 47
  • 1
  • 9