Python Articles

Page 357 of 855

How to set the border color of the dots in matplotlib's scatterplots?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 5K+ Views

To set the border color of dots in matplotlib scatterplots, use the edgecolors parameter in the scatter() method. This parameter controls the color of the dot borders, while linewidth controls their thickness. Basic Syntax plt.scatter(x, y, edgecolors='color_name', linewidth=width) Example with Red Border Here's how to create a scatterplot with red borders around the dots ? import numpy as np import matplotlib.pyplot as plt # Generate sample data N = 10 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) # Create scatterplot with red borders plt.scatter(x, y, s=500, c=colors, ...

Read More

Python - Add a prefix to column names in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 377 Views

A Pandas DataFrame allows you to add prefixes to all column names using the add_prefix() method. This is useful for distinguishing columns when merging DataFrames or organizing data. Syntax DataFrame.add_prefix(prefix) Parameters: prefix − String to add before each column name Creating a DataFrame First, let's create a DataFrame with car data ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], ...

Read More

Matplotlib – Date manipulation to show the year tick every 12 months

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 3K+ Views

Matplotlib provides powerful date formatting capabilities for time series visualization. To display year ticks every 12 months with proper month labeling, we use YearLocator and MonthLocator with custom formatters. Understanding Date Locators and Formatters Matplotlib's date handling uses two key components: Locators − Determine where ticks are placed on the axis Formatters − Control how dates are displayed as text For year ticks every 12 months, we set major ticks for years and minor ticks for individual months. Complete Example Here's how to create a time series plot with year ticks showing ...

Read More

Python - Reverse the column order of the Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 836 Views

To reverse the column order in a Pandas DataFrame, use the slice notation [::-1] with dataFrame.columns. This creates a new DataFrame with columns in reverse order without modifying the original data. Syntax dataFrame[dataFrame.columns[::-1]] Step-by-Step Process Import Required Library import pandas as pd Create a DataFrame import pandas as pd # Create a DataFrame with 4 columns dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, ...

Read More

Fetch only capital words from DataFrame in Pandas

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 231 Views

In Pandas, you can extract only capital words from a DataFrame using regular expressions. The re module provides pattern matching capabilities to identify words containing uppercase letters. Setting Up the Data First, let's create a sample DataFrame with mixed case words ? import re import pandas as pd # Create sample data with mixed case words data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'], ['KEYBOARD', 'charger', 'SMARTTV', 'camera']] df = pd.DataFrame(data, columns=['Col1', 'Col2', 'Col3', 'Col4']) print("Original DataFrame:") print(df) Original DataFrame: ...

Read More

Python - Display True for infinite values in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 374 Views

When working with numerical data in Pandas, you may encounter infinite values. You can identify and display True for infinite values using the isin() method or np.isinf() function. Creating a DataFrame with Infinite Values First, let's create a DataFrame containing some infinite values using np.inf − import pandas as pd import numpy as np # Create DataFrame with infinite values data = {"Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, 900, np.inf]} dataFrame = pd.DataFrame(data) print("DataFrame...") print(dataFrame) DataFrame... Reg_Price 0 7000.506 1 ...

Read More

Python Pandas – Check and Display row index with infinity

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

When working with Pandas DataFrames, you may need to identify rows containing infinity values. This is useful for data cleaning and analysis. Python provides np.isinf() and any() methods to check and display row indexes with infinity values. Required Libraries First, import the necessary libraries ? import pandas as pd import numpy as np Creating DataFrame with Infinity Values Create a DataFrame containing infinity values using np.inf ? import pandas as pd import numpy as np # Create dictionary with infinity values data = {"Reg_Price": [7000.5057, np.inf, 5000, np.inf, 9000.75768, 6000, ...

Read More

Python Pandas – Count the Observations

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

In Pandas, you can count the observations (rows) within groups using the groupby() method combined with count(). This is useful for analyzing the frequency of categories in your data. Creating a Sample DataFrame Let's start by creating a DataFrame with product information ? import pandas as pd # Create a DataFrame with product data dataFrame = pd.DataFrame({ 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, 25, 50] ...

Read More

Python Pandas - Sort DataFrame in ascending order according to the element frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 929 Views

When working with DataFrames, you may need to sort data based on how frequently elements appear. This can be achieved by combining groupby(), count(), and sort_values() methods. Syntax To sort DataFrame in ascending order according to element frequency ? df.groupby(['column']).count().reset_index(name='Count').sort_values(['Count'], ascending=True) Creating the DataFrame First, let's create a DataFrame with car data ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000], "Place": ...

Read More

Python - Calculate the maximum of column values of a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 617 Views

To find the maximum value in Pandas DataFrame columns, use the max() function. This method works on individual columns or across the entire DataFrame to identify the highest values. Basic DataFrame Creation First, let's create a sample DataFrame with car data ? import pandas as pd # Create DataFrame with car data car_data = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("Car DataFrame:") print(car_data) Car DataFrame: Car ...

Read More
Showing 3561–3570 of 8,546 articles
« Prev 1 355 356 357 358 359 855 Next »
Advertisements