How to generate BAR plot using pandas DataFrame?

How to generate BAR plot using pandas DataFrame?

How to generate BAR plot using pandas DataFrame?

This recipe helps you generate BAR plot using pandas DataFrame


Recipe Objective

visualizing a dataset give us a overall view of the data. It gives various statistical description about the data.

So this is the recipe on how we can generate BAR plot using pandas DataFrame. .

Step 1 - Importing Library

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

We have only imported pandas, numpy and matplotlib.pyplot which is needed.

Step 2 - Creating DataFrame

We have created a Dictionary and passed it through pd.DataFrame to create dataframe with different features. raw_data = {"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"], "pre_score": [4, 24, 31, 2, 3], "mid_score": [25, 94, 57, 62, 70], "post_score": [5, 43, 23, 23, 51]} df = pd.DataFrame(raw_data, columns = ["first_name", "pre_score", "mid_score", "post_score"]) print(df)

Step 3 - Creating Bar Plot

We have done various steps to plot bar graph. First we have assigned labels to the bar, then the y and horizontal position of the the bar. Bar graph is ploted by the function plt.barh and finally labelling the x, y axis and the graph. Molly = df.ix[1, 1:] Tina = df.ix[2, 1:] bar_labels = ["Pre Score", "Mid Score", "Post Score"] plt.figure(figsize=(8,6)) y_pos = np.arange(len(Molly)) y_pos = [x for x in y_pos] plt.yticks(y_pos, bar_labels, fontsize=10) plt.barh(y_pos, Molly, align="center", alpha=0.4, color="#263F13") plt.barh(y_pos, -Tina, align="center", alpha=0.4, color="#77A61D") plt.xlabel("Tina"s Score: Light Green. Molly"s Score: Dark Green") plt.title("Comparison of Molly and Tina"s Score") plt.ylim([-1,len(Molly)+0.1]) plt.xlim([-max(Tina)-10, max(Tina)+10]) plt.grid(); So the output comes as

Relevant Projects

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.