Python : displaying the number of a value in the columns in a bar graph

Viewed 28

In the dataframe below, each column has an "equal" value. I need a bar graph that shows how many of these values are relative to the columns. Column names on the x plane and the total number of "equal" on the y plane. Unfortunately, I can't show it because I can't print it. thanks.

   result_invoiceNo result_totalGross  ... result_totalNet result_invoiceDate
0              equal             equal  ...           equal              equal
1                                       ...                                   
2              equal             equal  ...           equal              equal
3                                       ...                                   
4              equal             equal  ...           equal              equal
..               ...               ...  ...             ...                ...
183                                     ...                          not equal
184            equal             equal  ...           equal              equal
185                                     ...                                   
186            equal             equal  ...           equal              equal
                                                                
1 Answers

csv**, I have gone through your above question and got the solution for it, if all the columns of dataframe is contains with 'equal' or 'not equal' data. that is below.

import pandas as pd

#create a DataFrame 
dt_frame = pd.DataFrame({'Column_A':['equal','equal','not equal','equal','equal','equal','equal','equal','not equal','equal'],
                        'Column_B':['not equal','equal','equal','equal','not equal','equal','equal','equal','not equal','equal'],
                        'Column_C':['equal','equal','equal','equal','equal','not equal','equal','equal','not equal','equal']})

#review the DataFrame
dt_frame.head()

head() returns the top five elements from DataFrame by default. Top 5 records of dt_frame

Convert your dataframe into below as well as.

columns = dt_frame.columns  # list of the columns of dataframe
# creating a new dataframe with two columns one is 'equal' and second is 'not equal' with columns as index.
pd_dt = pd.DataFrame({'equal':[None],'not equal':[None]}, index=columns)
pd_dt  #review the new dataframe.

pd_dt

Now count the values from every column of dt_frame and put the count in pd_dt columns.

for c in columns:
    pd_dt.loc[c]=dt_frame[c].value_counts()
pd_dt  # review the again dataframe.

final pd_dt Now, plot the bar graph, pandas provides the plot function to plot the dataframe.

pd_dt.plot(kind='bar', figsize=(10,10))

Output bar graph of dataframe

Related