Geek Logbook

Tech sea log book

Filtering and Counting Keys in Python Dictionaries

In this post, we’ll explore how to count the keys in a dictionary and filter a dictionary by its values in Python. These are common tasks that can be useful in a variety of situations when working with dictionaries. Counting the Keys of a Dictionary To count the number of keys in a dictionary, you

How to Aggregate Values by Date and Sum Them in Python

Problem Statement Suppose you have a list of transactions or events, each associated with a date and a numeric value (e.g., sales amount, transaction amount). Your goal is to aggregate these values by date and calculate the total sum for each date. Solution Approach We’ll use Python’s dictionary data structure to achieve this. Dictionaries allow

Combining Multiple CSV Files into One with Python

If you work with data, chances are you’ve encountered situations where you need to merge multiple CSV files into a single file for analysis. Manually combining these files can be time-consuming and error-prone. In this post, I’ll show you how to automate this task using Python and the powerful pandas library. Problem Statement Let’s assume

Creating Directories in Python

The os Module Python’s os module provides a way to interact with the operating system. It includes functions for creating, removing, and checking the existence of directories and files. In this tutorial, we’ll use the os.path.exists and os.makedirs functions. This module contains the necessary functions to check for the existence of a directory and create

Pandas Dataframe: apply method

Calculating Discounts, Taxes, and Total Amount in a DataFrame Suppose you have the following data in a DataFrame: Product Price Category 0 A 100 Electronic 1 B 200 Cloth 2 C 150 Electronic 3 D 300 Colth 4 E 250 Electronic In this DataFrame, you want to create three new columns: discount, taxes, and total_amount.

 Escape sequences in Pyhon

Escape sequences as in C While reading Automate the Boring Stuff with Python: Practical Programming for Total Beginners, I noticed the existence of raw strings. A raw string is created in such a way that escape sequences listed in the Python documentation are not processed. In fact, at the beginning of the section on Lexical

Dictionary methods: keys(), values(), and items()

In Python, there are three special methods related to dictionaries that are worth mentioning: keys(), values(), and items(). Interestingly, these methods do not return true lists. They cannot be modified and do not have an append() method. However, dict_keys, dict_values, and dict_items can be used in for loops. This distinction is important to keep in

Sellenium Vs Beautiful Soup

Web scraping is a widely recognized strategy for acquiring information. Before diving into this process, it’s crucial to familiarize oneself with two essential tools. Personally, this topic initially posed a significant challenge for me when attempting to extract data from the web. These tools serve distinct purposes in web scraping and automation. Selenium is primarily