Found 26504 Articles for Server Side Programming

Python Pandas - Fill NaN values using an interpolation method

AmitDiwan
Updated on 28-Sep-2021 11:47:43

2K+ Views

Use the interpolate() method to fill NaN values. Let’s say the following is our CSV file opened in Microsoft Excel with some NaN values −Load data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")Fill NaN values with interpolate() −dataFrame.interpolate()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("DataFrame...", dataFrame) # fill NaN values with interpolate() res = dataFrame.interpolate() print("DataFrame after interpolation...", res) OutputThis will produce the following output −DataFrame...        Car   Reg_Price   Units 0      BMW ... Read More

Python Pandas – Propagate non-null values backward

AmitDiwan
Updated on 28-Sep-2021 11:41:04

247 Views

Use the “method” parameter of the fillna() method. For backward fill, use the value ‘bfill’ as shown below −fillna(method='bfill')Let’s say the following is our CSV file opened in Microsoft Excel with some NaN values −At first, import the required library −import pandas as pdLoad data from a CSV file into a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") 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("DataFrame...", dataFrame) # propagate non null values backward res = dataFrame.fillna(method='bfill') print("DataFrame after backward fill...", res)OutputThis will produce the ... Read More

Python Pandas - Plot a Grouped Horizontal Bar Chart will all the columns

AmitDiwan
Updated on 28-Sep-2021 11:25:34

1K+ Views

For a grouped Horizontal Bar Chart with all the columns, create a Bar Chart using the barh() and do not set the a and y values.At 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 grouped Horizontal Bar Chart with all the columns −dataFrame.plot.barh(title='Car Specifications', color=("blue", "orange")) ExampleFollowing is the complete code − import pandas as pd import matplotlib.pyplot as plt # creating dataframe dataFrame = ... Read More

Draw a lineplot passing the entire dataset with Seaborn – Python Pandas

AmitDiwan
Updated on 28-Sep-2021 11:20:39

173 Views

Lineplot in Seaborn is used to draw a line plot with possibility of several semantic groupings. The seaborn.lineplot() is used for this. To plot lineplot with entire dataset, simply use the lineplot() and set the complete dataset in it without mentioning the x and y values.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")Plot lineplot with entire dataset −sb.lineplot(data=dataFrame) ExampleFollowing is the code −import ... Read More

Python Pandas - Create a Bar Plot and style the bars in Seaborn

AmitDiwan
Updated on 28-Sep-2021 11:12:32

254 Views

Bar Plot in Seaborn is used to show point estimates and confidence intervals as rectangular bars. The seaborn.barplot() is used. Style the bars using the facecolor, linewidth and edgecolor parameters.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") Design the bars −sb.barplot(x=dataFrame["Role"], y=dataFrame["Matches"], facecolor=(1, 1, 0, 0), linewidth=4, edgecolor=sb.color_palette("dark", 2))ExampleFollowing is the code − import seaborn as sb import pandas as pd import matplotlib.pyplot as ... Read More

Python Pandas – How to select DataFrame rows on the basis of conditions

AmitDiwan
Updated on 28-Sep-2021 11:01:52

508 Views

We can set conditions and fetch DataFrame rows. These conditions can be set using logical operators and even relational operators.At first, import the required pandas libraries −import pandas as pdLet us create a DataFrame and read our CSV file −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") Fetching dataframe rows with registration price less than 1000. We are using relational operator for this −dataFrame[dataFrame.Reg_Price < 1000]ExampleFollowing is the code −import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("DataFrame...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) ... Read More

Python - Renaming the columns of Pandas DataFrame

AmitDiwan
Updated on 27-Sep-2021 13:59:10

920 Views

To rename the columns of a DataFrame, use the rename() method. Set the column names you want to rename under the “columns” parameter of the rename() method. For example, changing “Car” column to “Car Name” −dataFrame.rename(columns={'Car': 'Car Name'}, inplace=False)At first, read the CSV and create a DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") Now, rename the column names. Here, we are renaming the columns “Car”, “Date_of_Purchase” and “Reg_Price” −dataFrame = dataFrame.rename(columns={'Car': 'Car Name', 'Date_of_Purchase': 'Sold On', 'Reg_Price' : 'Booking Price'}, inplace=False)ExampleFollowing is the codeimport pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("DataFrame...", dataFrame) # count the rows and ... Read More

Python - Remove the missing (NaN) values in the DataFrame

AmitDiwan
Updated on 27-Sep-2021 13:50:53

3K+ Views

To remove the missing values i.e. the NaN values, use the dropna() method. At first, let us import the required library −import pandas as pdRead the CSV and create a DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") Use the dropna() to remove the missing values. NaN will get displayed for missing values after dropna() is used −dataFrame.dropna()ExampleFollowing is the complete codeimport pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame with some NaN (missing) values...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) # drop ... Read More

Python - Find the Summary of Statistics of a Pandas DataFrame

AmitDiwan
Updated on 27-Sep-2021 13:41:37

373 Views

To find the summary of statistics of a DataFrame, use the describe() method. At first, we have imported the following pandas library with an aliasimport pandas as pdFollowing is our CSV file and we are creating a Pandas DataFrame −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")Now, get the summary of statistics of our Pandas DataFrame −dataFrame.describe()ExampleFollowing is the complete codeimport pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) # summary of DataFrame print("Get the summary of statistics ... Read More

Python Pandas – Count the rows and columns in a DataFrame

AmitDiwan
Updated on 27-Sep-2021 13:29:08

654 Views

To count the rows and columns in a DataFrame, use the shape property. At first, let’s say we have the a CSV file on the Desktop as shown in the below path −C:\Users\amit_\Desktop\CarRecords.csvRead the CSV file −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") Let us now count the rows and columns using shapedataFrame.shapeExampleFollowing is the code − import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame...", dataFrame) # count the rows and columns in a DataFrame print("Number of rows and column in our DataFrame = ", dataFrame.shape) # returns top 5 row records print("DataFrame with specific number of ... Read More

Advertisements