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
Programming Articles
Page 898 of 2547
Extracting rows using Pandas .iloc[] in Python
Pandas is a powerful Python library extensively used for data processing and analysis. The .iloc[] method allows you to select rows and columns from a DataFrame using integer-based indexing, making it perfect for positional data extraction. The iloc method uses zero-based indexing where the first row has index 0, second row has index 1, and so on. Similarly, columns are indexed starting from 0. Syntax DataFrame.iloc[row_indexer, column_indexer] Creating Sample Data Let's create a sample DataFrame to demonstrate iloc functionality ? import pandas as pd # Create sample DataFrame data = ...
Read MoreDuplicate substring removal from list in Python
Sometimes we may have a need to refine a given list by eliminating duplicate substrings from delimited strings. This can be achieved by using a combination of various methods available in Python's standard library like set(), split(), and list comprehensions. Using set() and split() The split() method segregates the elements for duplicate checking, and the set() method stores only unique elements from each string. This approach maintains grouping by original string ? Example # initializing list strings = ['xy-xy', 'pq-qr', 'xp-xp-xp', 'dd-ee'] print("Given list:", strings) # using set() and split() result = [set(sub.split('-')) ...
Read MoreCustom 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 More