Programming Articles - Page 1048 of 3363

Python Pandas - Draw a set of horizontal bar plots with Seaborn

AmitDiwan
Updated on 29-Sep-2021 08:54:39

838 Views

Bar Plot in Seaborn is used to show point estimates and confidence intervals as rectangular bars. The seaborn.barplot() is used for creating horizontal bar plots.Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required 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\Cricketers2.csv") Plotting horizontal bar plots −sb.barplot(x = "Matches", y = "Academy", data= dataFrame)ExampleFollowing is the code −import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data ... Read More

Python Pandas - Draw vertical bar plots with nested grouping by two categorical variables in Seaborn

AmitDiwan
Updated on 29-Sep-2021 08:50:21

889 Views

Bar Plot in Seaborn is used to show point estimates and confidence intervals as rectangular bars. The seaborn.barplot() is used for this. Plotting vertical bar plots grouped by a categorical variable, by passing categorical variables using x, y or hue parameter.Let’s say the following is our dataset in the form of a CSV file − Cricketers2.csvAt first, import the required 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\Cricketers2.csv") Plotting vertical bar plots grouped by two categorical variables. The hue parameter also setsb.barplot(x = ... Read More

Append list of dictionaries to an existing Pandas DataFrame in Python

AmitDiwan
Updated on 29-Sep-2021 08:42:23

1K+ Views

Append a list of dictionaries to an existing Pandas DataFrame, use the append() method. At first, create a DataFrame −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'XUV', 'Lexus', 'Volkswagen'], "Place": ['Delhi', 'Bangalore', 'Pune', 'Chandigarh', 'Mumbai'], "Units": [100, 150, 50, 110, 90] } ) Create list of Dictionaries − d = [{'Car': 'Mustang', 'Place': 'Hyderabad', 'Units': 60}, {'Car': 'Tesla', 'Place': 'Kerala', 'Units': 30}, {'Car': 'RollsRoyce', 'Place': 'Punjab', 'Units': 70}, {'Car': 'Bentley', 'Place': 'Gujarat', 'Units': 80}     ] Now, append list of Dictionaries to an already created DataFrame −dataFrame = dataFrame.append(d, ignore_index=True, ... Read More

Create a Pipeline and remove a column from DataFrame - Python Pandas

AmitDiwan
Updated on 29-Sep-2021 08:26:46

416 Views

Use the colDrop() method of pdpipe library to remove a column from Pandas DataFrame. At first, import the required pdpipe and pandas libraries with their respective aliases −import pdpipe as pdp import pandas as pdLet us create a DataFrame. Here, we have two columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )To remove a column from the DataFrame, use the ColDrop() method. Here, we are removing the “Units” column −resDF = pdp.ColDrop("Units").apply(dataFrame) ExampleFollowing is the complete code − import pdpipe as ... Read More

Python - Compute first of group values in a Pandas DataFrame

AmitDiwan
Updated on 29-Sep-2021 07:58:31

197 Views

To compute first of group values, use the groupby.first() method. At first, import the required library with an alias −import pandas as pd;Create a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'], "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'], "Units": [100, 150, 50, 80, 110, 90] } )Now, group DataFrame by a column −groupDF = dataFrame.groupby("Car") Compute first of group values and resetting index −res = groupDF.first() res = res.reset_index()ExampleFollowing is the complete code − import pandas as pd; dataFrame = pd.DataFrame(    {   ... Read More

How to extract the value names and counts from value_counts() in Pandas?

AmitDiwan
Updated on 29-Sep-2021 07:43:15

425 Views

To extract the value names and counts, let us first create a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Mustang', 'Tesla', 'Mustang', 'Mercedes', 'Tesla', 'Audi'], "Cubic Capacity": [2000, 1800, 1500, 2500, 2200, 3000, 2000], "Reg Price": [7000, 1500, 5000, 8000, 9000, 6000, 1500], "Units Sold": [ 200, 120, 150, 120, 210, 250, 220] })Fetch the value names and count for a specific column Car −res = dataFrame['Car'].value_counts() Fetch the value names and count for a specific column Units Sold −res = dataFrame['Units Sold'].value_counts()ExampleFollowing is the complete code −import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', ... Read More

Python Pandas – Merge DataFrame with one-to-many relation

AmitDiwan
Updated on 29-Sep-2021 07:35:38

3K+ Views

To merge Pandas DataFrame, use the merge() function. The one-to-many relation is implemented on both the DataFrames by setting under the “validate” parameter of the merge() function i.e. −validate = “one-to-many” or validate = “1:m”The one-to-many relation checks if merge keys are unique in left dataset.At first, let us create our 1st DataFrame −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Now, let us create our 2nd DataFrame −dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', ... Read More

Python Pandas – Merge and create cartesian product from both the DataFrames

AmitDiwan
Updated on 29-Sep-2021 07:21:01

2K+ Views

To merge Pandas DataFrame, use the merge() function. The cartesian product is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “cross”At first, let us import the pandas library with an alias −import pandas as pd Create DataFrame1 −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 120] } )Create DataFrame2dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Tesla', 'Jaguar'], "Reg_Price": [7000, 8000, 9000] } )Next, merge DataFrames with "cross" in "how" parameter i.e. ... Read More

Merge Pandas dataframe with a common column and set NaN for unmatched values

AmitDiwan
Updated on 29-Sep-2021 07:09:45

894 Views

To merge two Pandas DataFrame with common column, use the merge() function and set the ON parameter as the column name. To set NaN for unmatched values, use the “how” parameter and set it left or right. That would mean, merging left or right.At first, let us import the pandas library with an alias −import pandas as pdLet us create DataFrame1 −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Let us create DataFrame2dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', ... Read More

Python Pandas - Create Multiindex from dataframe

AmitDiwan
Updated on 29-Sep-2021 06:50:34

718 Views

To create Multiindex from DataFrame, use the MultiIndex. from_frame() method. At first, let us create a Dictionary of lists −d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22'] }Next, create a Pandas DataFrame from the above dictionary of lists −dataFrame = pd.DataFrame(d)Now create multiindex using from_frame() −print(pd.MultiIndex.from_frame(dataFrame))ExampleFollowing is the code −import pandas as pd # dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22']} # creating dataframe from the above dictionary of lists dataFrame = pd.DataFrame(d) print("DataFrame...", dataFrame) # creating ... Read More

Advertisements