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 by Tapas Kumar Ghosh
Page 6 of 19
How to Filter list elements starting with a given Prefix using Python?
Filtering list elements by prefix is a common task in Python programming. We can use several approaches including list comprehension, for loops, filter() function, or string slicing to accomplish this task. Let's understand with an example ? # Example: Filter names starting with "Am" names = ["Amelia", "Kinshuk", "Rosy", "Aman"] prefix = "Am" result = [name for name in names if name.startswith(prefix)] print(result) ['Amelia', 'Aman'] Key Methods Used startswith() Method The startswith() method returns True if a string starts with the specified prefix ? text = "Python programming" ...
Read MorePython - Filter odd elements from value lists in dictionary
Python dictionaries store key-value pairs where values can be lists. Filtering odd elements from these lists is a common task in data processing. An odd number is any integer that gives a remainder when divided by 2 (x % 2 != 0). For example − Given dictionary: {'A': [10, 21, 22, 19], 'B': [2, 5, 8]} After filtering odd elements: {'A': [21, 19], 'B': [5]} Understanding Odd Number Detection To identify odd numbers, we use the modulo operator ? # Two ways to check for odd numbers numbers = [1, 2, 3, 4, ...
Read MoreFilter Range Length Tuples in Python
Filtering tuples based on their length within a specific range is a common data processing task in Python. You can filter a list of tuples to keep only those with lengths between minimum and maximum values using various approaches like generator expressions, list comprehensions, loops, or filter functions. Using Generator Expression Generator expressions provide memory-efficient filtering by creating an iterator instead of storing all filtered results in memory ? def filter_range_tuples(tuples, min_len, max_len): return tuple(t for t in tuples if min_len
Read MoreConvert Lists to Nested Dictionary in Python
A nested dictionary is a dictionary that contains other dictionaries as values. Converting lists to nested dictionaries is useful for organizing hierarchical data structures. Python provides several approaches including loops, the reduce() function, dictionary comprehension, and recursion. Using for Loop The simplest approach uses a for loop to iterate through a list and create nested dictionaries ? my_list = ['a', 'b', 'c'] # Create empty dictionary and reference for nesting emp_dict = p_list = {} for item in my_list: p_list[item] = {} p_list = p_list[item] ...
Read MoreHow to select a range of rows from a dataframe in PySpark?
A PySpark DataFrame is a distributed collection of data organized into rows and columns. Selecting a range of rows means filtering data based on specific conditions. PySpark provides several methods like filter(), where(), and collect() to achieve this. Setting Up PySpark First, install PySpark and import the required modules ? pip install pyspark from pyspark.sql import SparkSession # Create SparkSession spark = SparkSession.builder \ .appName('DataFrame_Range_Selection') \ .getOrCreate() # Sample data customer_data = [ ("PREM KUMAR", 1281, "AC", 40000, 4000), ...
Read MoreHow to slice a PySpark dataframe in two row-wise dataframe?
PySpark dataframes can be split into two row-wise dataframes using various built-in methods. This process, called slicing, is useful for data partitioning and parallel processing in distributed computing environments. Syntax Overview The key methods for slicing PySpark dataframes include: limit(n) − Returns first n rows subtract(df) − Returns rows not present in another dataframe collect() − Retrieves all elements as a list head(n) − Returns first n rows as Row objects exceptAll(df) − Returns rows excluding another dataframe's rows filter(condition) − Filters rows based on conditions Installation pip install pyspark ...
Read MoreHow to set axes labels & limits in a Seaborn plot?
Seaborn automatically adjusts labels and axes limits to make plots more understandable, but sometimes you need custom control. Setting appropriate axes labels helps viewers understand what the plot represents, while adjusting limits lets you focus on specific data ranges. We can use matplotlib functions like xlabel(), ylabel(), xlim(), and ylim() to customize Seaborn plots. Core Functions for Axes Customization Here are the main functions used to set labels and limits: plt.xlabel() − Sets the x-axis label text plt.ylabel() − Sets the y-axis label text plt.xlim() − Sets the x-axis range limits plt.ylim() − Sets the y-axis ...
Read MoreHow to set the tab size in Text widget in Tkinter?
The Python Tkinter module provides a powerful way to create graphical user interfaces (GUIs). The Text widget is particularly useful for multi-line text input, and you can customize its tab size using the tabs parameter to improve text formatting and readability. Setting Tab Size in Text Widget The tabs parameter in the Text widget accepts a tuple or list of tab stop positions measured in pixels or other units ? import tkinter as tk # Create main window root = tk.Tk() root.title("Text Widget Tab Size") root.geometry("600x400") # Create Text widget with custom tab size ...
Read MoreHow to setup Conda environment with Jupyter Notebook?
Jupyter Notebook is an open-source web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text. Conda is a powerful package manager that helps you manage different Python environments and packages. Setting up a Conda environment with Jupyter Notebook provides an isolated workspace for your data science and machine learning projects. Benefits of Using Conda with Jupyter Notebook Create isolated environments for different projects with specific package versions Easy installation and management of data science packages like NumPy, Pandas, and Matplotlib Avoid package conflicts between different projects Simple environment sharing ...
Read MoreHow to set alignment of each dropdown widget in Jupyter?
Dropdown widgets in Jupyter notebooks can be aligned using CSS layout properties and the ipywidgets package. We can control alignment using the Layout() class to position dropdowns side by side, center them, or arrange them vertically for better visual presentation. Installation Requirements Install the required packages ? pip install ipywidgets ipyvuetify Basic Syntax The main components for creating aligned dropdown widgets ? # Create dropdown widget widgets.Dropdown(options=[], description='', layout=widgets.Layout()) # Define layout alignment widgets.Layout(width='70%', align_self='center') # Alternative using ipyvuetify v.Select(multiple=True, items=[], label='', style_='width:300px') Key Parameters ...
Read More