Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 350 of 855
Python Pandas – Propagate non-null values backward
In pandas, backward fill propagates non-null values backward to fill missing data. Use the fillna() method with method='bfill' to replace NaN values with the next valid observation. Syntax DataFrame.fillna(method='bfill') Creating Sample Data Let's create a DataFrame with missing values to demonstrate backward fill − import pandas as pd import numpy as np # Create sample data with NaN values data = { 'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang'], 'Reg_Price': [2500, 3500, 2500, 2000, 2500], 'Units': [100.0, np.nan, 120.0, np.nan, ...
Read MorePython Pandas - Plot a Grouped Horizontal Bar Chart will all the columns
A grouped horizontal bar chart displays multiple data series side by side horizontally. In Pandas, you can create this using the barh() method without specifying x and y parameters, which automatically uses all numeric columns. Setting Up the Data First, import the required libraries and create a DataFrame with multiple numeric columns ? import pandas as pd import matplotlib.pyplot as plt # Create DataFrame with car specifications dataFrame = pd.DataFrame({ "Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], ...
Read MorePython Pandas – How to select DataFrame rows on the basis of conditions
We can select DataFrame rows based on specific conditions using logical and relational operators. This is useful for filtering data to meet certain criteria. Creating Sample Data Let's create a DataFrame with car sales data to demonstrate conditional selection ? import pandas as pd # Create sample car sales data data = { 'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang', 'Lamborghini'], 'Date_of_Purchase': ['10/10/2020', '10/12/2020', '10/17/2020', '10/16/2020', '10/19/2020', '10/22/2020'], 'Reg_Price': [1000, 750, 750, 1500, 1100, 1000] } dataFrame = pd.DataFrame(data) print("Original DataFrame:") print(dataFrame) ...
Read MorePython - Remove the missing (NaN) values in the DataFrame
To remove missing values (NaN) from a DataFrame, use the dropna() method. This method removes rows or columns containing missing values based on your requirements. Creating a DataFrame with Missing Values First, let's create a DataFrame with some missing values to demonstrate the concept − import pandas as pd import numpy as np # Create a DataFrame with missing values data = { 'Car': ['Audi', 'Porsche', 'RollsRoyce', 'BMW', 'Mercedes'], 'Place': ['Bangalore', 'Mumbai', 'Pune', 'Delhi', 'Hyderabad'], 'UnitsSold': [80.0, np.nan, 100.0, np.nan, 80.0] } ...
Read MorePython - Find the Summary of Statistics of a Pandas DataFrame
The describe() method in Pandas provides a comprehensive statistical summary of numerical columns in a DataFrame. It calculates count, mean, standard deviation, minimum, maximum, and quartiles in one convenient method. Basic DataFrame Statistics First, let's create a sample DataFrame and get its statistical summary ? import pandas as pd # Create sample data data = { 'Car': ['Audi', 'Porsche', 'RollsRoyce', 'BMW', 'Mercedes', 'Lamborghini', 'Audi', 'Mercedes', 'Lamborghini'], 'Place': ['Bangalore', 'Mumbai', 'Pune', 'Delhi', 'Hyderabad', 'Chandigarh', 'Mumbai', 'Pune', 'Delhi'], 'UnitsSold': [80, 110, 100, 95, 80, 80, ...
Read MorePython Pandas – Count the rows and columns in a DataFrame
To count the rows and columns in a DataFrame, use the shape property. This returns a tuple where the first value is the number of rows and the second value is the number of columns. What is the shape Property? The shape property returns the dimensions of a DataFrame as a tuple (rows, columns). It's a quick way to understand the size of your dataset ? import pandas as pd # Create a sample DataFrame data = {'Car': ['Audi', 'Porsche', 'BMW', 'Mercedes'], 'Place': ['Bangalore', 'Mumbai', 'Delhi', 'Hyderabad'], ...
Read MorePython Pandas - Display specific number of rows from a DataFrame
To display specific number of rows from a DataFrame, use the head() function. Set the parameter to be the number of row records to be fetched. For example, for 10 rows, mention ? dataFrame.head(10) Basic Syntax The head() method displays the first n rows of a DataFrame ? DataFrame.head(n) Where n is the number of rows to display. If not specified, it defaults to 5. Creating Sample DataFrame Let's create a sample DataFrame to demonstrate ? import pandas as pd # Create sample data data = ...
Read MorePython Pandas - Iterate and fetch the rows that contain the desired text
To iterate and fetch rows containing desired text in a Pandas DataFrame, you can use the itertuples() method combined with string search operations. The itertuples() method iterates over DataFrame rows as named tuples. Basic Approach Using itertuples() and find() Let's create a sample DataFrame to demonstrate the concept ? import pandas as pd # Create sample car data data = { 'Car': ['BMW', 'Audi', 'Toyota', 'Mercedes', 'Honda', 'Lamborghini', 'Ford', 'Nissan', 'Lamborghini'], 'Place': ['Mumbai', 'Pune', 'Delhi', 'Bangalore', 'Chennai', 'Chandigarh', 'Kolkata', 'Hyderabad', 'Delhi'], 'UnitsSold': [120, ...
Read MoreFilter the rows – Python Pandas
In Python Pandas, filtering rows based on specific criteria is a common data manipulation task. The contains() method is particularly useful for filtering string columns by checking if they contain a specific substring. Basic Row Filtering with contains() The str.contains() method returns a boolean mask that can be used to filter DataFrame rows ? import pandas as pd # Create sample DataFrame data = { 'Car': ['Lamborghini', 'Ferrari', 'Lamborghini', 'Porsche', 'BMW'], 'Model': ['Huracan', 'F8', 'Aventador', '911', 'M3'], 'Year': [2020, 2021, 2019, 2020, 2018], ...
Read MoreHow to Sort CSV by a single column in Python ?
To sort a CSV file by a single column in Python, use the sort_values() method from Pandas. This method allows you to sort a DataFrame by specifying the column name and sort order. Syntax DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, na_position='last') Parameters by − Column name to sort by axis − 0 for rows, 1 for columns (default: 0) ascending − True for ascending, False for descending (default: True) inplace − Modify original DataFrame or return new one (default: False) na_position − Where to place NaN values: 'first' or 'last' (default: 'last') Example ...
Read More