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 443 of 2109
Write 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 MorePython program to filter perfect squares in a given series
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 MoreHow to use Python Pandas to find the total number of count for more than one special characters present in each word in a given series?
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 MoreWrite a program in Python to replace all the 0's with 5 in a given number
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 MoreMajority Element in Python
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 MoreWrite a program in Python to count the number of digits in a given number N
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 MoreCount Good Meals in Python
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 MoreBirthday Paradox in Python
The Birthday Paradox is a famous probability problem that asks: "How many people need to be in a room before there's a 50% chance that two people share the same birthday?" The counterintuitive answer is just 23 people! This paradox demonstrates how our intuition about probability can be misleading. Understanding the Mathematics The probability that two people have different birthdays is 364/365, which equals (1 - 1/365) in a non-leap year. For multiple people, the probability that all have different birthdays is: P(different) = 1 × (1-1/365) × (1-2/365) × (1-3/365) × ... Therefore, ...
Read MoreHow to set window size using phantomjs and selenium webdriver in Python?
We can set window size using PhantomJS and Selenium webdriver in Python. PhantomJS is a headless browser that allows automated testing without displaying a GUI. To work with PhantomJS, we create a driver object of the webdriver.PhantomJS class and pass the path to the phantomjs.exe driver file as a parameter. To set the window size, we use the set_window_size method and pass the width and height dimensions as parameters. We can also retrieve the current window size using the get_window_size method. Syntax driver.set_window_size(width, height) print(driver.get_window_size()) Parameters width − The desired window width ...
Read More