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 806 of 855
Custom Multiplication in list of lists in Python
Multiplying elements in a list of lists (nested list) with corresponding elements from another list is a common operation in data analysis. Python provides several approaches to perform this custom multiplication efficiently. Using Nested Loops In this approach, we use two nested for loops. The outer loop iterates through each sublist, while the inner loop multiplies each element in the sublist with the corresponding multiplier ? nested_list = [[2, 11, 5], [3, 2, 8], [11, 9, 8]] multipliers = [5, 11, 0] # Original list print("The given list:", nested_list) print("Multiplier list:", multipliers) # Using ...
Read MoreCustom length Matrix in Python
Sometimes when creating a matrix using Python, we may need to control how many times a given element is repeated in the resulting matrix. In this article, we will see how to create a matrix with a required number of elements when the elements are given as a list. Using zip() and List Comprehension We declare a list with elements to be used in the matrix. Then we declare another list which will hold the number of occurrences of the element in the matrix. Using the zip() function with list comprehension, we can create the resulting matrix ? ...
Read MoreCreating DataFrame from dict of narray-lists in Python
Pandas is a widely used Python library for data processing and analysis. In this article, we will see how to create pandas DataFrame from Python dictionaries containing lists as values. Creating DataFrame from Dictionary with Lists A dictionary with lists as values can be directly converted to a DataFrame using pd.DataFrame(). Each key becomes a column name, and the corresponding list becomes the column data. Example Let's create a DataFrame from a dictionary containing exam schedule information ? import pandas as pd # Dictionary for Exam Schedule exam_schedule = { ...
Read MoreCreating a Pandas dataframe column based on a given condition in Python
Pandas DataFrames allow you to add new columns based on conditions applied to existing data. This is useful for categorizing, labeling, or transforming data based on specific criteria. Creating the Base DataFrame Let's start with a simple exam schedule DataFrame ? import pandas as pd # Lists for Exam subjects and Days days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] subjects = ['Chemistry', 'Physics', 'Maths', 'English', 'Biology'] # Dictionary for Exam Schedule exam_data = {'Exam Day': days, 'Exam Subject': subjects} # ...
Read MoreCreate a Pandas Dataframe from a dict of equal length lists in Python
A Pandas DataFrame can be created from a dictionary where values are lists of equal length. This approach is useful when you have related data stored in separate lists and want to combine them into a tabular structure. Using Separate Lists and Dictionary In this approach, we declare lists individually and then use each list as a value for the appropriate key inside a dictionary. Finally, we apply pd.DataFrame() to convert the dictionary into a DataFrame ? Example import pandas as pd # Lists for Exam schedule days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] ...
Read MorePython to create a digital clock using Tkinter
Python Tkinter can be used to create all kinds of GUI programs for desktop applications. In this article, we will see how to create a digital clock that displays the current time in hours, minutes, and seconds format with live updates. We use the time module to import the strftime() method, which formats the current time. The clock updates automatically every 200 milliseconds using a recursive function and Tkinter's after() method. Creating the Digital Clock Here's how to build a simple digital clock using Tkinter ? import time from tkinter import * # Create ...
Read MoreAdd style to Python tkinter button
Tkinter has great support for creating GUI programs based on Python. It offers different ways of styling buttons on the Tkinter canvas based on font, size, color, and other properties. In this article, we will see how to apply styles to specific buttons or all buttons in general on the canvas using the ttk.Style class. Applying Style to Specific Buttons Let's consider the case when we have two buttons in the canvas and we want to apply styling only to the first button. We use a custom style name like 'W.TButton' as part of the configuration along with ...
Read MorePython - Filtering data with Pandas .query() method
The Pandas .query() method provides a powerful way to filter DataFrame rows using a string-based query expression. It offers a more readable alternative to boolean indexing for complex filtering conditions. Basic Syntax The .query() method accepts a query string and optional parameters ? import pandas as pd # Create sample dataset data = { 'age': [25, 30, 35, 40, 45, 50, 55, 60, 65], 'salary': [30000, 45000, 55000, 65000, 70000, 80000, 85000, 90000, 95000], 'department': ['IT', 'HR', 'IT', 'Finance', 'HR', 'IT', 'Finance', 'HR', ...
Read MorePython - Filter out integers from float numpy array
When working with NumPy arrays containing both floats and integers, you may need to filter out integer values for data cleansing purposes. This article demonstrates two effective methods to remove integers from a float NumPy array using astype comparison and modulo operations. Method 1: Using astype Comparison The astype function converts array elements to integers. By comparing the original array with its integer-converted version, we can identify which elements are not integers ? Example import numpy as np # Create array with mixed floats and integers data_array = np.array([3.2, 5.5, 2.0, 4.1, 5]) ...
Read MorePython - Filter dictionary key based on the values in selective list
Sometimes in a Python dictionary we may need to filter out certain keys based on a selective list. This allows us to extract specific key-value pairs from a larger dictionary. In this article we will see how to filter dictionary keys using different approaches. Using List Comprehension with 'in' Operator In this approach we put the keys to be filtered in a list. Then iterate through each element of the list and check for its presence in the given dictionary. We create a resulting dictionary containing only these filtered key-value pairs ? Example # Original ...
Read More