Programming Articles - Page 1041 of 3363

Python Pandas - Create a Horizontal Bar Chart

AmitDiwan
Updated on 01-Oct-2021 12:39:56

952 Views

To plot a Horizontal Bar Plot, use the pandas.DataFrame.plot.barh. A bar plot shows comparisons among discrete categories.At first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltCreate a Pandas DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 110, 150, 80, 200, 90] })Plot Horizontal Bar Chart using the plot.barh() −dataFrame.plot.barh(x='Car', y='Cubic_Capacity', title='Car Specifications', color='blue') ExampleFollowing is the complete code −import pandas as pd import matplotlib.pyplot as plt # creating dataframe dataFrame = pd.DataFrame({"Car": ['Bentley', ... Read More

Create a Count Plot with SeaBorn – Python Pandas

AmitDiwan
Updated on 01-Oct-2021 12:37:16

1K+ Views

Count Plot in Seaborn is used to display the counts of observations in each categorical bin using bars. The seaborn.countplot() is used for this.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required 3 libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") ExampleFollowing is the code −import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") ... Read More

Create a Point plot with SeaBorn – Python Pandas

AmitDiwan
Updated on 01-Oct-2021 12:33:51

372 Views

The seaborn.pointplot() is used to create a point plot. Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required 3 libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting point plot with Age and Weight column −sb.pointplot(x =dataFrame["Age"], y = dataFrame["Weight"], data = dataFrame)ExampleFollowing is the code −import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a Pandas DataFrame dataFrame = ... Read More

Create a Bar plot with SeaBorn – Python Pandas

AmitDiwan
Updated on 01-Oct-2021 12:31:10

1K+ Views

Bar Plot in Seaborn is used to show point estimates and confidence intervals as rectangular bars. The seaborn.barplot() is used for this.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required 3 libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting bar plot with Height column −sb.barplot(x =dataFrame["Height"])ExampleFollowing is the code − import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into ... Read More

Create a Swarm Plot with SeaBorn – Python Pandas

AmitDiwan
Updated on 01-Oct-2021 12:28:57

510 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() is used for this.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required 3 libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting swarm plot with Age and Height (inches) −sb.swarmplot(x = dataFrame["Age"], y = dataFrame["Height"], data=dataFrame)ExampleFollowing is the code −import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data from ... Read More

Python - Name columns explicitly in a Pandas DataFrame

AmitDiwan
Updated on 01-Oct-2021 12:26:53

337 Views

To name columns explicitly, use the names parameter of the read_csv() method. Let’s say the following is our CSV file without headers opened in Microsoft Excel −Let us load data from CSV file and with that add header columns using the names parameter −pd.read_csv("C:\Users\amit_\Desktop\TeamData.csv", names=['Team', 'Rank_Points', 'Year'])ExampleFollowing is the complete code −import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\TeamData.csv") print("Reading the CSV file without headers...", dataFrame) # adding headers dataFrame = pd.read_csv("C:\Users\amit_\Desktop\TeamData.csv", names=['Team', 'Rank_Points', 'Year']) # reading updated CSV print("Reading the CSV file after updating headers...", dataFrame)OutputThis ... Read More

Plot the dataset to display Horizontal Trend – Python Pandas

AmitDiwan
Updated on 01-Oct-2021 12:23:15

397 Views

Horizontal trend is also called Stationery trend. Let’s say the following is our dataset i.e. SalesRecords3.csvAt first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords3.csv") Casting column to datetime object −dataFrame['Sold_On'] = pd.to_datetime(dataFrame['Sold_On'])Create the plot for downtrend −dataFrame.plot() ExampleFollowing is the code −import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords3.csv") print("Reading the CSV file...", dataFrame) # casting column to datetime object dataFrame['Sold_On'] = pd.to_datetime(dataFrame['Sold_On']) dataFrame = dataFrame.set_index('Sold_On') ... Read More

Create a Violin Plot with SeaBorn – Python Pandas

AmitDiwan
Updated on 01-Oct-2021 12:20:40

691 Views

Violin Plot in Seaborn is used to draw a combination of boxplot and kernel density estimate. The seaborn.violinplot() is used for this.Let’s say the following is our dataset in the form of a CSV file − Cricketers.csvAt first, import the required 3 libraries −import seaborn as sb import pandas as pd import matplotlib.pyplot as pltLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers.csv") Plotting violin plot with column Weight (kgs) −sb.violinplot(dataFrame['Weight'])ExampleLet us see an example −import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into ... Read More

Python Pandas - Plot a Stacked Horizontal Bar Chart

AmitDiwan
Updated on 01-Oct-2021 12:17:47

2K+ Views

For a stacked Horizontal Bar Chart, create a Bar Chart using the barh() and set the parameter “stacked” as True −Stacked = TrueAt first, import the required libraries −import pandas as pd import matplotlib.pyplot as pltCreate a DataFrame with 3 columns −dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], })Plotting stacked Horizontal Bar Chart with all the columns −dataFrame.plot.barh(stacked=True, title='Car Specifications', color=("orange", "cyan")) ExampleFollowing is the complete code −import pandas as pd import matplotlib.pyplot as plt # creating dataframe dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', ... Read More

Evaluate the sum of rows using the eval() function – Python Pandas

AmitDiwan
Updated on 01-Oct-2021 12:14:59

516 Views

The eval() function can also be used to evaluate the sum of rows with the specified columns. At first, let us create a DataFrame with Product records −dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]})Finding sum using eval(). The resultant column with the sum is also mentioned in the eval(). The expression displays the sum formulae assigned to the resultant column −dataFrame = dataFrame.eval('Result_Sum = Opening_Stock + Closing_Stock')ExampleFollowing is the complete code −import pandas as pd dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, ... Read More

Advertisements