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
Python Articles
Page 617 of 855
How to select a Subset Of Data Using lexicographical slicingin Python Pandas?
Pandas provides powerful indexing capabilities to select subsets of data. Lexicographical slicing allows you to select data based on alphabetical ordering of string indexes, similar to how words are arranged in a dictionary. Loading and Exploring the Dataset Let's start by importing a movies dataset and examining its structure − import pandas as pd import numpy as np movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv", index_col="title", ...
Read MoreHow to select subset of data with Index Labels in Python Pandas?
Pandas provides powerful selection capabilities to extract subsets of data using either index positions or index labels. This article demonstrates how to select data using index labels with the .loc accessor. The .loc attribute works similar to Python dictionaries, selecting data by index labels rather than positions. This is different from .iloc which selects by integer position like Python lists. Setting Up the Dataset Let's start by importing a movies dataset with the title as the index ? import pandas as pd movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv", ...
Read MoreHow to Find The Largest Or Smallest Items in Python?
Finding the largest or smallest items in a collection is a common task in Python. This article explores different methods to find single or multiple largest/smallest values efficiently. Method 1: Using min() and max() for Single Items For finding a single smallest or largest item (N=1), min() and max() are the most efficient functions ? import random # Create a random list of integers random_list = random.sample(range(1, 10), 9) print("List:", random_list) # Find the smallest number smallest = min(random_list) print("Smallest:", smallest) # Find the largest number largest = max(random_list) print("Largest:", largest) ...
Read MoreHow to Identify Most Frequently Occurring Items in a Sequence with Python?
When analyzing sequences of data, identifying the most frequently occurring items is a common task. Python's Counter from the collections module provides an elegant solution for counting and finding the most frequent elements in any sequence. What is a Counter? The Counter is a subclass of dictionary that stores elements as keys and their counts as values. Unlike regular dictionaries that raise a KeyError for missing keys, Counter returns zero for non-existent items. from collections import Counter # Regular dictionary raises KeyError regular_dict = {} try: print(regular_dict['missing_key']) except KeyError as e: ...
Read MoreSelenium and Python to find elements and text?
We can find elements and extract their text with Selenium webdriver. First, identify the element using any locator like id, class name, CSS selector, or XPath. Then use the text property to obtain the text content. Syntax element_text = driver.find_element(By.CSS_SELECTOR, "h4").text Here driver is the webdriver object. The find_element() method identifies the element using the specified locator, and the text property extracts the text content. Modern Selenium Approach Recent Selenium versions use the By class for locators instead of the deprecated find_element_by_* methods ? from selenium import webdriver from selenium.webdriver.common.by import ...
Read MoreHow to set Selenium Python WebDriver default timeout?
Setting default timeout in Selenium Python WebDriver helps prevent tests from hanging indefinitely. Selenium provides two main approaches: set_page_load_timeout() for page loading and implicitly_wait() for element location. Page Load Timeout The set_page_load_timeout() method sets a timeout for page loading. If the page doesn't load within the specified time, a TimeoutException is thrown. Syntax driver.set_page_load_timeout(timeout_in_seconds) Example from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.common.exceptions import TimeoutException # Setup WebDriver service = Service() driver = webdriver.Chrome(service=service) try: # Set page load timeout to 10 seconds ...
Read MoreWait until page is loaded with Selenium WebDriver for Python.
We can wait until the page is loaded with Selenium WebDriver using synchronization concepts. Selenium provides implicit and explicit wait mechanisms. To wait until the page is loaded, we use the explicit wait approach. The explicit wait depends on expected conditions for particular element behaviors. For waiting until the page loads, we use expected conditions like presence_of_element_located for a specific element. If the wait time elapses without the condition being met, a timeout error is thrown. Required Imports To implement explicit wait conditions, we need the WebDriverWait and expected_conditions classes ? from selenium import webdriver ...
Read MoreProgram to check whether given list is in valid state or not in Python
Sometimes we need to check if a list can be completely partitioned into valid groups. This problem involves grouping numbers using specific rules to determine if the entire list is in a "valid state". Problem Definition Given a list of numbers, check if every number can be grouped using one of these rules: Contiguous pairs: (a, a) − two identical numbers Identical triplets: (a, a, a) − three identical numbers Consecutive triplets: (a, a+1, a+2) − three consecutive numbers Example For nums = [7, 7, 3, 4, 5], we can group [7, 7] ...
Read MoreProgram to find all upside down numbers of length n in Python
An upside down number (also called a strobogrammatic number) is a number that appears the same when rotated 180 degrees. The digits that remain valid when rotated are: 0, 1, 6, 8, and 9, where 6 becomes 9 and 9 becomes 6 when rotated. So, if the input is like n = 2, then the output will be ['11', '69', '88', '96']. Understanding Valid Digits When rotated 180 degrees ? 0 → 0 1 → 1 6 → 9 ...
Read MoreProgram to check all values in the tree are same or not in Python
Suppose we have a binary tree, we have to check whether all nodes in the tree have the same values or not. This is a common tree traversal problem that can be solved using recursive depth-first search. So, if the input is like ? 5 5 5 5 5 ...
Read More