Pandas Articles

Page 22 of 42

Write a program in Python to resample a given time series data and find the maximum month-end frequency

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 250 Views

Time series resampling in Pandas allows you to change the frequency of your data and apply aggregation functions. In this tutorial, we'll learn how to resample time series data to find the maximum month-end frequency using the resample() method. Understanding the Problem We have a time series dataset with weekly data points and want to group them by month, then find the maximum values for each month. The result will show month-end dates with the corresponding maximum values. Step-by-Step Solution Step 1: Create the DataFrame First, we'll create a DataFrame with ID numbers and generate ...

Read More

Write a Python program to read an Excel data from file and read all rows of first and last columns

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 1K+ Views

Reading specific columns from an Excel file is a common data analysis task. Python's pandas library provides the iloc method to select rows and columns by their position, making it easy to extract the first and last columns from any dataset. Understanding Column Selection with iloc The iloc method uses integer-based indexing where: df.iloc[:, 0] selects all rows of the first column (index 0) df.iloc[:, -1] selects all rows of the last column (index -1) The colon (:) means "all rows" Creating Sample Data First, let's create a sample Excel file to demonstrate ...

Read More

Write a program in Python to read CSV data from a file and print the total sum of last two rows

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 2K+ Views

When working with CSV files in Python, you often need to perform calculations on specific rows. This tutorial shows three different methods to read CSV data and calculate the sum of the last two rows using pandas. Sample CSV Data First, let's create a sample CSV file named pandas.csv: Id, Data 1, 11 2, 22 3, 33 4, 44 5, 55 6, 66 7, 77 8, 88 9, 99 10, 100 Using tail() Method The tail() method returns the last n rows of a DataFrame. Combined with sum(), it provides a clean solution ...

Read More

Write a Python program to export dataframe into an Excel file with multiple sheets

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 977 Views

Exporting a Pandas DataFrame to an Excel file with multiple sheets is a common requirement for data analysis and reporting. Python provides several approaches to accomplish this using libraries like xlsxwriter, openpyxl, or Pandas' built-in Excel writer. Using xlsxwriter Engine The xlsxwriter engine provides excellent formatting options and performance for creating Excel files ? import pandas as pd import xlsxwriter # Create sample DataFrame df = pd.DataFrame({ 'Fruits': ["Apple", "Orange", "Mango", "Kiwi"], 'City': ["Shimla", "Sydney", "Lucknow", "Wellington"] }) print("Original DataFrame:") print(df) # Create Excel writer ...

Read More

Write a Python program to separate a series of alphabets and digits and convert them to a dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 210 Views

When working with mixed alphanumeric data in Pandas, you often need to separate alphabetic and numeric parts into different columns. This is commonly done using the str.extract() method with regular expressions. Problem Statement Given a Pandas Series containing strings with both letters and digits, we need to separate them into two columns in a DataFrame ? Original Series: 0 abx123 1 bcd25 2 cxy30 dtype: object Expected DataFrame: 0 1 0 abx 123 1 bcd ...

Read More

Write a program in Python to filter armstrong numbers in a given series

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 379 Views

An Armstrong number (also called a narcissistic number) is a number that equals the sum of its digits raised to the power of the number of digits. For 3-digit numbers, each digit is cubed and summed. In this tutorial, we'll filter Armstrong numbers from a Pandas Series. What is an Armstrong Number? For a 3-digit number, if the sum of cubes of its digits equals the original number, it's an Armstrong number: 153: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ✓ 371: 3³ + 7³ + 1³ = 27 + ...

Read More

Write a Python program to shuffle all the elements in a given series

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 334 Views

When working with Pandas Series, you might need to shuffle the elements to randomize their order. Python provides multiple approaches to shuffle a series: using random.shuffle() directly or implementing a manual shuffle algorithm. Using random.shuffle() The simplest approach is to use Python's built-in random.shuffle() method, which shuffles the series elements in-place ? import pandas as pd import random as rand data = pd.Series([1, 2, 3, 4, 5]) print("The original series is:") print(data) rand.shuffle(data) print("The shuffled series is:") print(data) The original series is: 0 1 1 ...

Read More

Write a program in Python to covert the datatype of a particular column in a dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 171 Views

In Pandas, you can convert the datatype of a specific column using the astype() method. This is useful when you need to change data types for analysis, memory optimization, or data consistency. Creating a Sample DataFrame Let's start by creating a DataFrame with mixed data types ? import pandas as pd data = {'Name': ['David', 'Adam', 'Bob', 'Alex', 'Serina'], 'Age': [13, 12, 12, 13, 12], 'Maths': [98, 59, 66, 95, 70], ...

Read More

Write a Python code to swap last two rows in a given dataframe

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 705 Views

In Pandas, you can swap the last two rows of a DataFrame using iloc indexing. This technique temporarily stores one row, assigns the other row's values, and then restores the stored row. Approach To swap the last two rows, we follow these steps ? Store the last row in a temporary variable using df.iloc[-1] Assign the second-to-last row values to the last row position Assign the temporary row data to the second-to-last position Example Let's create a DataFrame and swap its last two rows ? import pandas as pd data ...

Read More

Write a program in Python to remove one or more than one columns in a given DataFrame

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 252 Views

Sometimes you need to remove one or more columns from a Pandas DataFrame. Python provides several methods to accomplish this: using del statement, pop() method, and drop() method. Using del Statement The del statement permanently removes a single column from the DataFrame ? import pandas as pd data = [[1, 2, 3], [4, 5, 6]] df = pd.DataFrame(data, columns=('one', 'two', 'three')) print("Before deletion:") print(df) del df['one'] print("After deletion:") print(df) Before deletion: one two three 0 1 2 ...

Read More
Showing 211–220 of 418 articles
« Prev 1 20 21 22 23 24 42 Next »
Advertisements