AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 81 of 840

Python Pandas - Iterate and fetch the rows that contain the desired text

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 296 Views

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 More

How to Sort CSV by a single column in Python ?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 7K+ Views

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

Python - Read all CSV files in a folder in Pandas?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 7K+ Views

Reading all CSV files from a folder is a common data processing task. Python's glob module combined with Pandas' read_csv() method provides an efficient solution for batch processing multiple CSV files. Setting Up the File Path First, we need to specify the directory path containing our CSV files. For this example, we'll use a relative path that works across different systems ? import pandas as pd import glob import os # Set the path to your CSV files directory path = "data/" # Using relative path for better portability Finding CSV Files ...

Read More

Python Pandas - Draw a violin plot and set quartiles as horizontal lines with Seaborn

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 642 Views

A violin plot combines a box plot and kernel density estimation to show the distribution of data. In Seaborn, you can draw violin plots with quartiles displayed as horizontal lines using the inner="quartile" parameter. What is a Violin Plot? A violin plot displays the probability density of data at different values, similar to a box plot but with a rotated kernel density plot on each side. The quartiles help identify the median and interquartile range within the distribution. Basic Violin Plot with Sample Data Let's create a violin plot using sample data to demonstrate the quartile ...

Read More

Python Pandas - Draw a swarm plot and control swarm order by passing an explicit order with Seaborn

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 724 Views

A swarm plot in Seaborn creates a categorical scatterplot with non-overlapping points, making it ideal for visualizing the distribution of values across categories. You can control the order of categories using the order parameter to customize how data appears on the plot. Creating Sample Data Let's create sample cricket data to demonstrate swarm plot ordering ? import seaborn as sns import pandas as pd import matplotlib.pyplot as plt # Create sample cricket data data = { 'Academy': ['Victoria', 'Western Australia', 'South Australia', 'Victoria', ...

Read More

Python Pandas - Group the swarms by two categorical variables with Seaborn

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 488 Views

Swarm Plot in Seaborn is used to draw a categorical scatterplot with non-overlapping points. The seaborn.swarmplot() function is used for this. To group the swarms by two categorical variables, set those variables in the swarmplot() using the x, y or hue parameters. Sample Dataset We'll create a sample cricket dataset to demonstrate grouping by two categorical variables ? import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Create sample cricket data data = { 'Role': ['Batsman', 'Batsman', 'Bowler', 'Bowler', 'All-rounder', 'All-rounder', ...

Read More

Python Pandas - Draw a violin plot and control order by passing an explicit order with Seaborn

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 477 Views

A violin plot in Seaborn combines a boxplot with a kernel density estimate to show data distribution. The seaborn.violinplot() function creates these plots, and you can control the category order using the order parameter. Creating Sample Data Let's create sample cricket data to demonstrate violin plots ? import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Create sample cricket data data = { 'Role': ['Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', 'Bowler', 'Batsman', ...

Read More

How to Merge multiple CSV Files into a single Pandas dataframe ?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To merge multiple CSV files into a single Pandas DataFrame, you can use pd.concat() with pd.read_csv(). This approach efficiently combines data from multiple files while preserving the structure. Basic Setup First, import the required Pandas library ? import pandas as pd Creating Sample CSV Data Let's create sample CSV files to demonstrate the merging process ? import pandas as pd import io # Create sample data for first CSV csv1_data = """Car, Place, UnitsSold Audi, Bangalore, 80 Porsche, Mumbai, 110 RollsRoyce, Pune, 100""" # Create sample data for second ...

Read More

Python Pandas – Create a subset and display only the last entry from duplicate values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To create a subset and display only the last entry from duplicate values, use the drop_duplicates() method with the keep parameter set to 'last'. This method removes duplicate rows based on specified columns and keeps only the last occurrence of each duplicate. Creating the DataFrame Let us first create a DataFrame with duplicate entries ? import pandas as pd # Create DataFrame with duplicate Car-Place combinations dataFrame = pd.DataFrame({ 'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], ...

Read More

Python – Merge two Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 896 Views

To merge two Pandas DataFrames, use the merge() function. By default, it performs an inner join on common columns between the DataFrames. Basic Syntax pd.merge(left_df, right_df, on='column_name', how='inner') Creating Sample DataFrames First, let's create two DataFrames with a common column ? 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 ...

Read More
Showing 801–810 of 8,392 articles
« Prev 1 79 80 81 82 83 840 Next »
Advertisements