Python Articles

Page 544 of 855

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 1K+ 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 218 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 387 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 351 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 177 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 730 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 272 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

Write a program in Python to find the most repeated element in a series

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

Finding the most repeated element in a pandas Series is a common data analysis task. We'll explore different approaches to solve this problem efficiently. Sample Series Let's start with a sample Series to demonstrate the concept ? import pandas as pd data_list = [1, 22, 3, 4, 22, 5, 22] series = pd.Series(data_list) print("Series is:") print(series) Series is: 0 1 1 22 2 3 3 4 4 22 5 5 ...

Read More

Write a program in Python to select any random odd index rows in a given DataFrame

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

In data analysis, you often need to randomly select rows from a DataFrame based on certain criteria. This tutorial shows how to select a random row from odd-indexed positions (1, 3, 5, etc.) in a Pandas DataFrame. Sample DataFrame Let's start with a sample DataFrame to demonstrate the concept ? import pandas as pd import random df = pd.DataFrame({ 'id': [1, 2, 3, 4, 5, 6, 7], 'mark': [70, 60, 40, 50, 80, 90, 60], 'age': [12, 13, 12, 13, 12, 13, 12] ...

Read More
Showing 5431–5440 of 8,546 articles
« Prev 1 542 543 544 545 546 855 Next »
Advertisements