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
Server Side Programming Articles
Page 434 of 2109
Write a Python program to export dataframe into an Excel file with multiple sheets
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 MoreWrite a Python program to separate a series of alphabets and digits and convert them to a dataframe
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 MoreWrite a program in Python to filter armstrong numbers in a given series
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 MoreWrite a Python program to shuffle all the elements in a given series
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 MoreWrite a program in Python to covert the datatype of a particular column in a dataframe
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 MoreWrite a Python code to swap last two rows in a given dataframe
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 MoreWrite a program in Python to remove one or more than one columns in a given DataFrame
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 MoreWrite a program in Python to find the most repeated element in a series
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 MoreWrite a program in Python to select any random odd index rows in a given DataFrame
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 MoreWrite a program in Python to merge two dataframes based on matching data in a column
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