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
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
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
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance