I am new to loops, and I am trying to iterate over all items in a list, and I need to generate the values between 0 and 2 with a given step value. I have tried to use the “range” function, but cannot get it to work. The end result should look something like this (doesn’t have to be in
Tag: python
Is there a way to combine the following two list comprehensions?
I have a list of “point_objects”, from which I build locx and locy. Please see below. Can these two list comprehensions be brought to just a single line? Answer From the title, I understood that you want one line that computes both of these. You can use zip():
Pivot matrix to time-series – Python
I’ve got a dataframe with date as first column and time as the name of the other columns. Date 13:00 14:00 15:00 16:00 … 2022-01-01 B R M M … 2022-01-02 B B B M … 2022-01-03 R B B M … How could I transform that matrix into a datetime time-series? My objective its something like t…
Search substrings in strings and return relevant string when matched
I have a dataframe with product titles, which contain keywords, that can identify the product type as such: df_product_titles dataframe I have another dataframe with two columns, where the 1st column has the keyword and the relevant product type: df_product_types dataframe I want to search each keyword from p…
Python: when trying to extract certain keys, how can I avoid a KeyError when in some dict elements, the key value is missing from APi json?
I can successfully extract every column using Python, except the one I need most (order_id) from an API generated json that lists field reps interactions with clients. Not all interactions result in orders; there are multiple types of interactions. I know I will need to add the flag to show ‘None’…
How do I make an list that can be read as either uppercase or lowercase
Im attempting to rewrite an old program of mine, and Im trying to us a list (as an array) to organize the values better than a bunch of variables. I got the array set up, and then set up a if elif else to check input against the values in the array, but then figured I should try and make
Concat pandas dataframes in Python with different row size without getting NaN values
I have to combine some dataframes in Python. I’ve tried to combine them using concat operation, but I am getting NaN values because each dataframe has different row size. For example: In this example, dataframe 1 and dataframe 2 only have 1 row. However, dataframe 3 has 3 rows. When I combine these 3 da…
Is there a bug in Python 3.8 datetime with DST transitions?
I’m trying to convert a timezone-aware datetime in Europe/Sofia to the start of the day in Europe/Sofia, but returning the datetime in UTC. Doing this, I encountered a strange problem: Which prints: The bug happens when subtracting the timedelta or doing the replace(), because the result should be midni…
Pandas long format of success table
I have a table with the following structure in pandas: I would like to put it in a long format. In this case, we have, for each user, a different number of events, and successes. I would like to transform this into an event table (each row corresponds to an event, and there is a column that tells you whether
Why does pandas.DataFrame.merge return dataframes with different column types than the input dataframes?
Slightly expanding the Example 1: Merge on Multiple Columns with Different Names, results in the following Python code using Pandas pandas.DataFrame.merge: The resulting output (I’ve added line numbers): Notice the type of a2 and d columns in the resulting df_merge dataframe on lines 24 through 27 have …