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
Articles by AmitDiwan
Page 101 of 840
Python - Create a Pipeline in Pandas
To create a pipeline in Pandas, we use the pipe() 100] def add_category(df): df['CATEGORY'] = 'Premium' return df # Create DataFrame df = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) # Chain multiple operations result = (df.pipe(uppercase_columns) .pipe(filter_high_units) .pipe(add_category)) print("Final result after pipeline:") print(result) ...
Read MorePython Pandas and Numpy - Concatenate multiindex into single index
To concatenate a multiindex into a single index in Pandas, we can use the map() method with join() to combine tuple elements with a separator. Let us start by importing the required libraries ? Import Libraries import pandas as pd import numpy as np Creating a Series with Tuple Index First, we create a Pandas Series with tuples as index values ? # Create tuples for multiindex index_tuples = [('Jacob', 'North'), ('Ami', 'East'), ('Ami', 'West'), ...
Read MorePython - Typecasting Pandas into set
To typecast Pandas DataFrame columns into a set, use the set() function. This is useful for removing duplicates and performing set operations like union, intersection, and difference. Creating a DataFrame Let us first create a DataFrame with employee data ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'], "Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North'] } ) print("DataFrame:") print(dataFrame) DataFrame: ...
Read MorePython Pandas - Finding the uncommon rows between two DataFrames
To find the uncommon rows between two DataFrames, you can use concat() combined with drop_duplicates(). This approach concatenates both DataFrames and removes duplicate rows, leaving only the uncommon ones. Syntax pd.concat([df1, df2]).drop_duplicates(keep=False) Where keep=False removes all occurrences of duplicated rows, leaving only the unique rows from each DataFrame. Example Let's create two DataFrames with car data and find the uncommon rows ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Reg_Price": [1000, 1500, ...
Read MorePython - How to Concatenate Two or More Pandas DataFrames along rows?
To concatenate two or more Pandas DataFrames along rows, use the pd.concat() method with axis=0. This combines DataFrames vertically, stacking them on top of each other. Syntax pd.concat([df1, df2, df3, ...], axis=0) Creating Sample DataFrames First, let's create three sample DataFrames to demonstrate concatenation − import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Col1": [10, 20, 30], "Col2": [40, 50, 60], "Col3": [70, 80, 90] }, index=[0, 1, 2]) print("DataFrame1:") print(dataFrame1) DataFrame1: ...
Read MorePython Pandas – Check if two Dataframes are exactly same
The equals() method is used to check if two DataFrames are exactly the same. It compares both the structure and content of DataFrames, including data types, column names, and index values. Syntax DataFrame.equals(other) Parameters: other − Another DataFrame to compare with Returns: True if DataFrames are identical, False otherwise Example 1: Different DataFrames Let us create two different DataFrames and check if they are equal − import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], ...
Read MorePython Pandas – Find the Difference between two Dataframes
Finding differences between two DataFrames in Pandas involves comparing their structure, values, and content. The equals() method checks for exact equality, while other methods help identify specific differences. Creating Sample DataFrames Let's create two DataFrames to demonstrate comparison techniques ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) DataFrame1: Car Units 0 ...
Read MorePython – Create a Subset of columns using filter()
To create a subset of columns in Pandas, we can use the filter() method. This allows us to filter columns with similar patterns using the like parameter, or select specific columns using indexing. Creating a DataFrame First, let's create a sample DataFrame with product information ? import pandas as pd dataFrame = pd.DataFrame({ "Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("DataFrame...") print(dataFrame) DataFrame... Closing_Stock Opening_Stock ...
Read MorePython program to construct Equidigit tuples
Equi-digit tuples are tuples where a number is split into two halves at the middle position. Python provides an efficient way to construct these using the floor division operator // and string slicing. Understanding Equi-digit Tuples An equi-digit tuple divides a number into two parts of equal or nearly equal length. For even-digit numbers, both parts have equal digits. For odd-digit numbers, the first part has one fewer digit than the second part. Example Below is a demonstration of constructing equi-digit tuples ? my_list = [5613, 1223, 966143, 890, 65, 10221] print("The list ...
Read MorePython program to omit K length Rows
When working with lists of lists, you may need to filter out rows based on their length. This Python program demonstrates how to omit rows that have exactly K elements using iteration and the len() method. Example Below is a demonstration of the same − my_list = [[41, 7], [8, 10, 12, 8], [10, 11], [6, 82, 10]] print("The list is :") print(my_list) my_k = 2 print("The value of K is") print(my_k) my_result = [] for row in my_list: if len(row) != my_k : ...
Read More