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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Write a program in Python to remove the elements in a series, if it contains exactly two spaces
Sometimes you need to filter out pandas Series elements based on specific criteria. This tutorial shows how to remove elements that contain exactly two spaces using different approaches. Sample Data Let's start with a pandas Series containing text data ? import pandas as pd text_data = ["This is pandas", "python script", "pandas series"] data = pd.Series(text_data) print("Original Series:") print(data) Original Series: 0 This is pandas 1 python script 2 pandas series dtype: object Method 1: Using String count() Method ...
Read MoreWrite a program in Python to sort all the elements in a given series in descending order
Sorting a Pandas Series in descending order is a common data manipulation task. We can use the sort_values() method with the ascending=False parameter to achieve this. Creating a Sample Series First, let's create a Pandas Series with string elements ? import pandas as pd data_list = ["abdef", "ijkl", "Abdef", "oUijl"] series = pd.Series(data_list) print("Original Series:") print(series) Original Series: 0 abdef 1 ijkl 2 Abdef 3 oUijl dtype: object Sorting in Descending Order Use the sort_values() method ...
Read MoreWrite a program in Python to verify kth index element is either alphabet or number in a given series
In this tutorial, we'll learn how to verify whether the kth index element in a Pandas Series contains alphabetic characters or numeric digits. This is useful for data validation and type checking operations. Input − Assume, you have a Series: a abc b 123 c xyz d ijk Solution To solve this, we will follow the steps given below − Define a Series with mixed data types Get the index from user input Use string methods to check if the ...
Read MoreWrite a program in Python to print the elements in a series between a specific range
When working with Pandas Series, you often need to filter elements that fall within a specific range. Python provides several methods to accomplish this task efficiently. Input − Assume, you have a series: 0 12 1 13 2 15 3 20 4 19 5 18 6 11 Output − The result for the elements between 10 to 15: 0 12 1 13 2 ...
Read MoreWrite a program in Python to count the total number of integer, float and object data types in a given series
When working with Pandas Series, you may need to analyze the data types of elements. This program demonstrates how to count integer, float, and string data types in a given series using lambda functions and filters. Problem Statement Input − Assume, you have a series, 0 1 1 2 2 python 3 3 4 4 5 5 6 6.5 Output − Total number of integer, float and string elements are, integer ...
Read MoreWrite a program in Python to check if a series contains duplicate elements or not
A Pandas Series is a one-dimensional data structure that can contain duplicate values. To check if a series contains duplicates, we can compare the series length with the number of unique elements. Sample Series Data Let's start with a series that has no duplicate elements ? import pandas as pd import numpy as np # Series with no duplicates data = pd.Series([1, 2, 3, 4, 5]) print("Original Series:") print(data) Original Series: 0 1 1 2 2 3 3 4 4 ...
Read MoreWrite a program in Python to filter only integer elements in a given series
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 MoreWrite a program in Python to replace all odd index position in a given series by random uppercase vowels
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 MoreWrite a program in Python to filter valid dates in a given series
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 MoreWrite a program in Python to generate five random even index lowercase alphabets in a series
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