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
Programming Articles
Page 320 of 2547
Python - Search DataFrame for a specific value with pandas
Searching a DataFrame for specific values is a common operation in data analysis. Pandas provides several methods to locate and retrieve rows based on specific criteria using boolean indexing and iloc. Creating the DataFrame First, let's create a sample DataFrame with car information ? import pandas as pd # Creating DataFrame with car data dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": ...
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 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 MoreHow to display Pandas Dataframe in Python without Index?
In Pandas, DataFrames display with an index by default. You can hide the index when displaying using to_string(index=False) or when printing to console using various methods. Creating a Sample DataFrame Let's start by creating a DataFrame with custom index and column labels ? import pandas as pd # Create DataFrame with custom index dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], ...
Read More