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
Python Articles
Page 357 of 855
How to set the border color of the dots in matplotlib's scatterplots?
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 MorePython - Add a prefix to column names in a Pandas DataFrame
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 MoreMatplotlib – Date manipulation to show the year tick every 12 months
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 MorePython - Reverse the column order of the Pandas DataFrame
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 MoreFetch only capital words from DataFrame in Pandas
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 MorePython - Display True for infinite values in a Pandas DataFrame
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 MorePython Pandas – Check and Display row index with infinity
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 MorePython Pandas – Count the Observations
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 MorePython Pandas - Sort DataFrame in ascending order according to the element frequency
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 MorePython - Calculate the maximum of column values of a Pandas DataFrame
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