Server Side Programming Articles

Page 434 of 2109

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

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 976 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 333 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

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

Vani Nalliappan
Vani Nalliappan
Updated on 25-Mar-2026 407 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 766 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

Write a program in Python to merge two dataframes based on matching data in a column

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

In data analysis, you often need to combine data from multiple DataFrames based on common columns. Pandas provides the merge() function to join DataFrames similar to SQL joins. Understanding DataFrame Merging When you have two DataFrames with a common column, you can merge them to create a single DataFrame containing matching records. The merge operation finds rows where the specified column values match between both DataFrames. Creating Sample DataFrames Let's start by creating two DataFrames with a common 'id' column ? import pandas as pd # First DataFrame first_df = pd.DataFrame({ ...

Read More
Showing 4331–4340 of 21,090 articles
« Prev 1 432 433 434 435 436 2109 Next »
Advertisements