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 Rishikesh Kumar Rishi
Page 14 of 102
Selecting with complex criteria from a Pandas DataFrame
We can use different criteria to compare all the column values of a Pandas DataFrame. We can perform comparison operations like df[col] < 5, df[col] == 10, etc. For example, if we use the criteria df[col] > 2, then it will check all the values from col and compare whether they are greater than 2. For all the column values, it will return True if the condition holds, else False. Basic Comparison Operations Example Let's create a DataFrame and apply various comparison criteria ? import pandas as pd df = pd.DataFrame( ...
Read MoreGroup-by and Sum in Python Pandas
The groupby() and sum() functions in Pandas allow you to group data by specific columns and calculate the sum of numeric values for each group. This is particularly useful for data aggregation and analysis. Basic Group-by and Sum Here's how to group data by a single column and sum the values ? import pandas as pd # Create sample data df = pd.DataFrame({ "Category": ["A", "B", "A", "B", "A"], "Sales": [100, 150, 200, 120, 80], "Profit": [20, 30, 40, 25, 15] }) ...
Read MoreHow to get column index from column name in Python Pandas?
To get column index from column name in Python Pandas, we can use the get_loc() method on the DataFrame's columns Index object. Syntax df.columns.get_loc(column_name) Parameters column_name − The name of the column whose index you want to find Return Value Returns an integer representing the positional index of the specified column. Example Let's create a DataFrame and find the index of different columns ? import pandas as pd # Create a DataFrame df = pd.DataFrame( { ...
Read MoreHow to find the common elements in a Pandas DataFrame?
Finding common elements between Pandas DataFrames is essential for data analysis tasks like identifying shared records or performing data validation. Python provides several methods including merge(), intersection(), and set operations. Using merge() Method The merge() method performs an inner join by default, returning only common rows between DataFrames − import pandas as pd df1 = pd.DataFrame({ "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] }) df2 = pd.DataFrame({ "x": [5, 2, ...
Read MoreHow to create a 100% stacked Area Chart with Matplotlib?
A 100% stacked area chart displays data as percentages of the total, where each area shows the relative contribution of each category. In Matplotlib, we use stackplot() with percentage-normalized data to create this visualization. Understanding 100% Stacked Area Charts Unlike regular stacked area charts that show absolute values, a 100% stacked chart normalizes all values to percentages, making it easier to compare proportional relationships over time. Creating a 100% Stacked Area Chart Here's how to create a 100% stacked area chart showing world population distribution by continent ? import matplotlib.pyplot as plt import numpy ...
Read MoreHow to understand Seaborn's heatmap annotation format?
Seaborn's heatmap annotation format controls how numeric values are displayed on each cell of the heatmap. The fmt parameter accepts Python string formatting codes to customize the appearance of annotations. Basic Heatmap with Default Annotations Let's start with a simple heatmap to see default annotation behavior ? import seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt # Create sample data data = pd.DataFrame(np.random.random((4, 4)), columns=['A', 'B', 'C', 'D']) ...
Read MoreHow to remove or hide X-axis labels from a Seaborn / Matplotlib plot?
To remove or hide X-axis labels from a Seaborn / Matplotlib plot, you can use several methods. The most common approach is using set(xlabel=None) on the axes object. Using set(xlabel=None) This method removes the X-axis label while keeping the tick labels ? import matplotlib.pyplot as plt import seaborn as sns # Set figure size plt.rcParams["figure.figsize"] = [8, 4] plt.rcParams["figure.autolayout"] = True # Set Seaborn style sns.set_style("whitegrid") # Load example dataset tips = sns.load_dataset("tips") # Create boxplot and remove X-axis label ax = sns.boxplot(x="day", y="total_bill", data=tips) ax.set(xlabel=None) plt.show() Using set_xlabel("") ...
Read MoreHow to remove whitespaces at the bottom of a Matplotlib graph?
When creating Matplotlib plots, you may notice unwanted whitespace at the bottom or around your graph. Python provides several methods to remove this whitespace and create cleaner, more professional-looking plots. Steps to Remove Whitespace Set the figure size and adjust the padding between and around the subplots Create a new figure or activate an existing figure Add an subplot to the figure with proper scaling parameters Plot your data points using the plot() method Apply whitespace removal techniques like tight_layout() or autoscale_on=False Display the figure using show() method Method 1: Using autoscale_on=False This method ...
Read MoreHow to extract only the month and day from a datetime object in Python?
To extract only the month and day from a datetime object in Python, you can use several approaches including the strftime() method, direct attribute access, or DateFormatter() for matplotlib plots. Using strftime() Method The strftime() method formats datetime objects into readable strings − from datetime import datetime # Create a datetime object dt = datetime(2023, 7, 15, 14, 30, 0) # Extract month and day using strftime() month_day = dt.strftime("%m-%d") print("Month-Day:", month_day) # With month name month_day_name = dt.strftime("%B %d") print("Month Day:", month_day_name) Month-Day: 07-15 Month Day: July 15 ...
Read MoreHow to remove the first and last ticks label of each Y-axis subplot in Matplotlib?
When creating multiple subplots in Matplotlib, you might want to remove the first and last tick labels from the Y-axis to create cleaner visualizations. This can be achieved by iterating through the axes and setting specific tick labels to invisible. Method: Using setp() to Hide Tick Labels The most effective approach is to use plt.setp() to modify the visibility of specific tick labels ? import matplotlib.pyplot as plt import numpy as np # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create subplots with sample data fig, ax = ...
Read More