List comprehension conditionals for datetime objects

Advertisements I am writing the following function that returns the last day of every month between two given dates: import datetime as dt def dates_list_last_day_of_month(start_date=(2000,1,1),end_date=(2023,4,26)): start_date = dt.date(*start_date) end_date = dt.date(*end_date) def last_day_of_month(year, month): next_month = dt.date(year, month, 1) + dt.timedelta(days=32) return next_month.replace(day=1) – dt.timedelta(days=1) last_days = [] for year in range(start_date.year,end_date.year+1): for month in… Read More List comprehension conditionals for datetime objects

pandas categorical remove categories from multiple columns

Advertisements I have many categorical columns like: df = pd.DataFrame( data={ "id": [1, 2, 3, 4], "category1": [" ", "data", "more data", " "], "category2": [" ", "more data", " ", "and more"], } ) df["category1"] = df["category1"].astype("category") df["category2"] = df["category2"].astype("category") I want to remove any levels of the categorical type columns that only have… Read More pandas categorical remove categories from multiple columns

TypeError: 'int' object is not iterable with zip()

Advertisements the question goes like this: revenue = [30000, 50000, 70000, 90000] cost = [10000, 15000, 20000, 30000] Write a comprehension together with the zip() function to make a new list that maintains profits. I tried doing it with for-loop: new_list = [] for revenue, cost in zip(revenue, cost): profit = revenue – cost new_list.append(profit)… Read More TypeError: 'int' object is not iterable with zip()

Compare two DataFrame columns of lists of strings (A & B) to find if any in B are NOT in A for adding to Col C

Advertisements d = {‘Col A’: [[‘Singapore’,’Germany’,’UK’],[‘Ireland’,’Japan’,’Australia’],[‘India’,’Korea’,’Vietnam’]], ‘Col B’: [[‘Singapore’,’Germany’,’UK’],[‘Ireland’,’Japan’],[‘India’,’Mexico’,’Argentina’]]} df = pd.DataFrame(data=d) I’m trying to compare these two columns and return a new column, Col C, that contains any strings that are present in Col B but NOT present in Col A. So row 1 being the same returns no value, row 2 where A… Read More Compare two DataFrame columns of lists of strings (A & B) to find if any in B are NOT in A for adding to Col C

What's the difference between the two list comprehension in Python?

Advertisements I just solved a question in LeetCode, however I found that the two different list comprehensions produce different results. So what’s the difference? ans = [[0] * (n-2)] * (n-2) ans = [[0] * (n-2) for _ in range(n-2)] The question is LeetCode 2373, and the two different results are as follows: This solution… Read More What's the difference between the two list comprehension in Python?

How to substitute a list of words based on a list of tuples by preserving the same original order?

Advertisements Given the following list of tokens: a = [‘heyyo’, ‘how’, ‘ale’, ‘yiou’] And a list of tuples: b = [(‘yiou’, ‘you’), (‘heyyo’, ‘hello’)] How can I replace the elements of the list a considering the elements of the list b? For example, the expected output would be: [‘hello’, ‘how’, ‘ale’, ‘you’] This is due… Read More How to substitute a list of words based on a list of tuples by preserving the same original order?