Say I have a pandas Series: I have to get a series (or array) of subrange maximum values. For example, a subrange of 5. For the first element, the value should be max{2, 0, 8, 0, 1} = 8. The second value should be max{0, 8, 0, 1, 2} = 8. Starting from the 8th element, there are less than
Tag: pandas
Applying custom function to groupby object keeps groupby column
I have a dataframe which as a column for grouping by and several other columns. Play dataframe: When using a groupby on this dataframe followed by a default function, the groupby column is set as an index and not included in the results: But when I define a custom function and use apply, I get an unwanted add…
Storing outputs from input parameters into the rows of a panda data frame
I have a list containing values that I want to perform mathematical operations on with different input parameters that are also stored in a list. For each value in the list, I want to store the results from performing the operation – with each distinct input parameter – row-by-row, where each row …
Python Pandas to_datetime Without Zero Padded
I am trying to convert a date & time string using Pandas ‘to_datetime’, but the string values is non-zero padded: I have the following but get a mismatch error Is there a way to add the zero padding or have ‘to_datetime’ accept the above formatting? Answer The trouble isn’t i…
Printing between two number ranges with a given step value
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
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…
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…
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 …
How to calculate the Successive Month difference with Groupby in pandas
I have the pandas dataframe, I need to Group by each id and then apply the monthly difference in each id to get monthly frequency number I tried out : Solution : I am expecting the Output dataframe : Answer You can use period objects to calculate the number of monthly periods in between 2 dates: output: