When working with Pandas Series that contain mixed data types, you often need to filter only the integer elements. Python provides several approaches to accomplish this task using type checking, regular expressions, or built-in Pandas methods. Sample Data Let's start by creating a series with mixed data types ? import pandas as pd data_list = [1, 2, "python", "pandas", 3, 4, 5] series = pd.Series(data_list) print("Original Series:") print(series) Original Series: 0 1 1 2 2 ... Read More
In this tutorial, we'll learn how to replace values at odd index positions in a Pandas Series with random uppercase vowels. This is useful for data masking or creating test datasets. Problem Statement Given a Series with numeric values, we need to replace all values at odd index positions (1, 3, 5, etc.) with random uppercase vowels (A, E, I, O, U). Input 0 1 1 2 2 3 3 4 4 5 dtype: int64 Expected Output ... Read More
Filtering valid dates from a Pandas Series involves identifying date strings that follow a specific format. We'll explore two approaches: using regular expressions with loops and using Pandas filtering methods. Sample Data Let's start with a Series containing date strings in various formats ? import pandas as pd dates = ['2010-03-12', '2011-3-1', '2020-10-10', '11-2-2'] data = pd.Series(dates) print("Original Series:") print(data) Original Series: 0 2010-03-12 1 2011-3-1 2 2020-10-10 3 11-2-2 dtype: object ... Read More
In this article, we'll explore how to generate five random even-indexed lowercase alphabets and create a Pandas Series from them. Even-indexed characters are those at positions 0, 2, 4, 6, etc. in the alphabet. Using String Slicing (Recommended) The most efficient approach uses string slicing with step 2 to extract even-indexed characters − import pandas as pd import string import random chars = string.ascii_lowercase print("Lowercase alphabets:", chars) # Extract even-indexed characters using slicing even_chars = list(chars[::2]) print("Even-indexed characters:", even_chars) # Generate 5 random samples data = random.sample(even_chars, 5) print("Random even-indexed characters:", data) ... Read More
A perfect square is a number that can be expressed as the product of an integer with itself. In this tutorial, we'll learn how to filter perfect squares from a Pandas Series using different approaches. Given a series with numbers, we want to identify and filter only the perfect squares ? Input and Expected Output Input − 0 14 1 16 2 30 3 49 4 80 Output − 1 16 3 ... Read More
When working with text data in Pandas, you might need to count words containing multiple special characters. This tutorial shows how to find the total count of words that have more than one special character in a given Series. Input Data Let's start with a sample series containing words with special characters ? import pandas as pd data = pd.Series(["fruits!!", "*cakes*", "$nuts", "#drinks"]) print(data) 0 fruits!! 1 *cakes* 2 $nuts 3 #drinks dtype: object ... Read More
Given an integer N, the task is to replace all the 0's that appear in the number with '5'. However, leading zeros are ignored as they don't affect the number's value. For example, if N = 1007, the output should be 1557. Example Cases Input-1 − N = 1007 Output − 1557 Explanation − The given number has 2 zeros which when replaced by '5' results in the output as 1557. Input-2 − N = 105 Output − 155 Explanation − ... Read More
The majority element is an element that appears more than half the times in an array. For example, in an array of size 8, a majority element must appear at least 5 times. Python provides several approaches to find the majority element efficiently. Problem Definition Given an array of integers, find the element that appears more than n/2 times, where n is the array size. If no such element exists, return -1. Example 1 ? Input: [2, 1, 1, 2, 2, 2] Output: 2 Explanation: Element 2 appears 4 times out of 6, which is ... Read More
Let's suppose we have given a number N. The task is to find the total number of digits present in the number. For example, Input-1 − N = 891452 Output − 6 Explanation − Since the given number 891452 contains 6 digits, we will return '6' in this case. Input-2 − N = 74515 Output − 5 Explanation − Since the given number 74515 contains 5 digits, we will print the output as 5. The Approach Used to Solve This Problem ... Read More
A good meal contains exactly two different food items with a sum of deliciousness equal to a power of two. You can pick any two different foods to make a good meal. Given an array of integers arr where arr[i] is the deliciousness of the ith item of food, we need to return the number of different good meals you can make from this list. Examples Input-1 − arr = [1, 3, 5, 7, 9] Output − 4 Explanation − The good meals are (1, 3), (1, 7), (3, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance