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
Server Side Programming Articles
Page 311 of 2109
Filter 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 MorePython - Read all CSV files in a folder in Pandas?
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 MorePython Pandas - Draw a violin plot and set quartiles as horizontal lines with Seaborn
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 MorePython Pandas - Draw a swarm plot and control swarm order by passing an explicit order with Seaborn
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 MorePython Pandas - Group the swarms by two categorical variables with Seaborn
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 MorePython Pandas - Draw a violin plot and control order by passing an explicit order with Seaborn
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 MoreHow to Merge multiple CSV Files into a single Pandas dataframe ?
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 MorePython Pandas – Create a subset and display only the last entry from duplicate values
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 MorePython – Merge two Pandas DataFrame
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