Found 26504 Articles for Server Side Programming

Python Pandas - Select a subset of rows from a dataframe

AmitDiwan
Updated on 29-Sep-2021 11:35:56

1K+ Views

To select a subset of rows, use conditions and fetch data.Let’s say the following are the contents of our CSV file opened in Microsoft Excel −At first, load data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")Let’s say we want the Car records with “Units” more than 100 i.e. subset of rows. For this, use −dataFrame[dataFrame["Units"] > 100] Now, let’s say we want the Car records with “Reg_Price” less than 100 i.e. subset of rows. For this, use −dataFrame[dataFrame["Reg_Price"] < 3000]ExampleFollowing is the code − import pandas as pd # Load data from a CSV file ... Read More

Python - How to select a subset of a Pandas DataFrame

AmitDiwan
Updated on 29-Sep-2021 11:23:19

375 Views

Let’s say the following are the contents of our CSV file opened in Microsoft Excel −At first, load data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")To select a subset, use the square brackets. Mention the column in the brackets and fetch single column from the entire dataset −dataFrame['Car'] ExampleFollowing is the code − import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("Reading the CSV file...", dataFrame) # displaying only a single column res1 = dataFrame['Car']; # displaying only a subset print("Displaying only one column ... Read More

Python - Plot a Pandas DataFrame in a Line Graph

AmitDiwan
Updated on 29-Sep-2021 11:16:12

2K+ Views

To plot a DataFrame in a Line Graph, use the plot() method and set the kind parameter to line. Let us first import the required libraries −import pandas as pd import matplotlib.pyplot as mpFollowing is our data with Team Records −data = [["Australia", 2500, 2021], ["Bangladesh", 1000, 2021], ["England", 2000, 2021], ["India", 3000, 2021], ["Srilanka", 1500, 2021]]Set the data as Pandas DataFrame and add columns −dataFrame = pd.DataFrame(data, columns=["Team", "Rank_Points", "Year"]) Plot the Pandas DataFrame in a line graph. We have set the “kind” parameter as “line” for this −dataFrame.plot(x="Team", y=["Rank_Points", "Year" ], kind="line", figsize=(10, 9))ExampleFollowing is the code −import ... Read More

Python - How to plot a Pandas DataFrame in a Bar Graph

AmitDiwan
Updated on 29-Sep-2021 09:06:36

3K+ Views

Let’s say the following are the contents of our CSV file −       Car Reg_Price 0      BMW 2000 1    Lexus 1500 2     Audi 1500 3   Jaguar 2000 4  Mustang 1500Import the required libraries −import pandas as pd import matplotlib.pyplot as mpOur CSV file is on the Desktop. Load data from a CSV file into a Pandas ... Read More

Python Pandas - Plot multiple data columns in a DataFrame?

AmitDiwan
Updated on 29-Sep-2021 09:02:34

2K+ Views

To plot multiple columns, we will be plotting a Bar Graph. Use the plot() method and set the kind parameter to bar for Bar Graph. Let us first import the required libraries −import pandas as pd import matplotlib.pyplot as mpFollowing is our data with Team Records −data = [["Australia", 2500, 2021], ["Bangladesh", 1000, 2021], ["England", 2000, 2021], ["India", 3000, 2021], ["Srilanka", 1500, 2021]]Set the data as Pandas DataFrame and add columns −dataFrame = pd.DataFrame(data, columns=["Team", "Rank_Points", "Year"]) Plot multiple columns in a bar graph. We have set the “kind” parameter as “bar” for this −dataFrame.plot(x="Team", y=["Rank_Points", "Year" ], kind="bar", figsize=(10, ... Read More

Python Pandas - Draw a Bar Plot and use median as the estimate of central tendency

AmitDiwan
Updated on 29-Sep-2021 08:58:45

2K+ 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 horizontal bar plots with dataset columns as x and y values. Use the estimator parameter to set median as the estimate of central tendency.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 plt from numpy import medianLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") Plotting horizontal bar plots with ... Read More

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

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

812 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

868 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

394 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

Advertisements