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 Kiran P
Page 3 of 11
How to compare files in Python
Python's filecmp module provides efficient methods to compare files and directories. It offers three main functions: cmp() for comparing individual files, cmpfiles() for comparing multiple files, and dircmp() for comprehensive directory comparison. Basic File Comparison with cmp() The filecmp.cmp() function compares two files and returns True if they are identical, False otherwise ? import filecmp import os # Create test files with open('file1.txt', 'w') as f: f.write('Hello World') with open('file2.txt', 'w') as f: f.write('Hello World') with open('file3.txt', 'w') as f: f.write('Different ...
Read MoreHow to scrape through Media Files in Python?
Scraping through media files in Python involves extracting data, metadata, or content from various media formats like images, audio, and video files. Python provides several libraries to work with different media types and extract useful information from them. Working with Image Files The PIL (Python Imaging Library) and its modern fork Pillow are commonly used for image processing and metadata extraction. from PIL import Image from PIL.ExifTags import TAGS import os # Create a sample image for demonstration img = Image.new('RGB', (100, 100), color='red') img.save('sample.jpg') # Load and extract basic information image = Image.open('sample.jpg') ...
Read MoreHow to select the largest of each group in Python Pandas DataFrame?
When analyzing data, you often need to find the row with the largest value in each group. This tutorial shows how to select the most popular movie for each year from a movies dataset using Python Pandas. Preparing the Dataset Let's start by loading a movies dataset and examining its structure ? import pandas as pd import numpy as np # Load movies dataset movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv") # Display sample rows print("Sample data:") print(movies.sample(n=3)) Sample data: budget id original_language original_title popularity ...
Read MoreHow to unpack using star expression in Python?
Python's star expression (*) allows you to unpack sequences without knowing their exact length in advance. This solves the limitation of traditional unpacking where you must match the number of variables to sequence elements. The Problem with Traditional Unpacking When unpacking sequences, you must know the exact number of elements ? random_numbers = [0, 1, 5, 9, 17, 12, 7, 10, 3, 2] random_numbers_descending = sorted(random_numbers, reverse=True) print(f"Sorted numbers: {random_numbers_descending}") # This will cause an error - too many values to unpack try: largest, second_largest = random_numbers_descending except ValueError as e: ...
Read MoreHow to perform Calculations with Dictionaries in Python?
Dictionaries in Python store key-value pairs, but performing calculations like finding minimum, maximum, or sorting requires special techniques since dictionaries don't have a natural ordering. Let's explore different approaches using tennis player data. Creating Sample Data We'll create a dictionary with tennis players and their Grand Slam titles ? player_titles = { 'Federer': 20, 'Nadal': 20, 'Djokovic': 17, 'Murray': 3, 'Thiem': 1, 'Zverev': 0 } print(player_titles) {'Federer': 20, ...
Read MoreHow to compare two DataFrames in Python Pandas with missing values
When working with DataFrames containing missing values, comparing data becomes challenging because NumPy's NaN values don't behave like regular values. Understanding how to properly compare DataFrames with missing data is essential for data analysis tasks. Understanding NaN Behavior NumPy NaN values have unique mathematical properties that differ from Python's None object ? import pandas as pd import numpy as np # Python None Object compared against self print(f"Python None == None: {None == None}") # Numpy nan compared against self print(f"np.nan == np.nan: {np.nan == np.nan}") # Is nan greater than numbers? print(f"np.nan ...
Read MoreHow to use the Subprocess Module in Python?
The subprocess module in Python allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This is the recommended way to execute system commands and interact with the operating system from Python programs. Understanding Processes When you execute a program, your Operating System creates a process. It uses system resources like CPU, RAM, and disk space. A process is isolated from other processes — it can't see what other processes are doing or interfere with them. Python's subprocess module provides a powerful interface for working with processes, allowing you to run ...
Read MoreHow to process iterators in parallel using ZIP
The zip() function allows you to iterate over multiple sequences in parallel, pairing elements by index. This is particularly useful for processing corresponding elements from different iterables simultaneously. Basic List Processing Example First, let's see a traditional approach to multiply each element by 5 − numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] multiply_by_5 = [] for x in numbers: multiply_by_5.append(x * 5) print(f"Output: {multiply_by_5}") Output: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] Using list comprehension, we can achieve the ...
Read MoreHow to process excel files data in chunks with Python?
Processing large Excel files can consume significant memory and slow down your Python applications. When dealing with Excel spreadsheets containing thousands of rows, loading the entire file into memory at once isn't always practical. This article demonstrates how to process Excel files in manageable chunks using Python and Pandas. Prerequisites Before working with Excel files in Python, you need to install the required libraries ? # Install required packages # pip install pandas openpyxl xlsxwriter import pandas as pd import xlsxwriter Creating Sample Excel Data First, let's create a sample Excel file to ...
Read MoreHow to Parse HTML pages to fetch HTML tables with Python?
Extracting HTML tables from web pages is a common task in web scraping and data analysis. Python provides powerful libraries like requests, BeautifulSoup, and pandas to make this process straightforward. Required Libraries First, install the necessary packages if they're not already available ? pip install requests beautifulsoup4 pandas tabulate Basic Setup Import the required libraries and set up the target URL ? import requests import pandas as pd from bs4 import BeautifulSoup from tabulate import tabulate # Set the target URL site_url = "https://www.tutorialspoint.com/python/python_basic_operators.htm" Making HTTP Request ...
Read More