I have the following dataframe: Different ‘type’ can occur at the same ‘time’, but the need is to only get the ‘type’ and ‘value’ based on the following conditions: priority 1: the type importance is so as t>o>f priority 2: highest value to be considered from value column I have tried using groupby and dictionary with: grp = merged_df.groupby([‘name’,’time’],as_index=False)[[‘type’,’value’]].apply(lambda x:
Tag: group-by
Group by calculation pandas
I have a dataframe after applying groupby: On this, I want to add a new column with the calculation: 10 / (no of items per category). For the example data, this would be: How can this be done? Answer Use Series.value_counts with Series.map: Or:
How to create a summary table with subheadings in SQL?
I am using PostgreSQL to create summaries from data using a python script. I had large amounts of data in my SQL table and using the following query I was able to get the required data. Below is my query: And below is the table created: I am trying to create a query on this table to further create a
Pandas groupby datetime columns by periods
I have the following dataframe: I would like to get for each row (e.g a,b,c,d …) the mean vale between specific hours. The hours are between 9-15, and I want to groupby period, for example to calculate the mean value between 09:00:00 to 11:00:00, between 11- 12, between 13-15 (or any period I decide to). I was trying first to
Groupby and count only how many times customer was called at specific point of time
my problem is closely related to Groupby count only when a certain value is present in one of the column in pandas. Let’s say I have a dataframe which is sorted by not_unique_id and date_of_call. Now I want to add a new column which tells me, how often the customer was called successfully in the past. In other words: count
pandas groupby column to list and keep certain values
I have the following dataframe: I create a new column with a list of the all the occupations: How do I only include teacher and student values in occupation_list? Answer You can filter before groupby: Output:
Django group by Choice Field and COUNT Zeros
Consider the following django model: Now, I’d like to group all Ratings by the number of ratings per category like this: which returns a QuerySets like this: Is there a way to include all not-rated dimensions? To achieve an output like: Answer First we will create a dictionary with counts for all dimensions initialised to 0. Next we will query
How to create files from a groupby object, based on the length of the dataframe
I have a dataframe (df) that looks like this (highly simplified): The ‘VALUE’ column contains a variable number of rows with identical values. I am trying to output a series of csv files that contain all of the rows that contain a ‘VALUE’ length == 2, ==3 etc. For example: I can get the desired output of one length value
Grouping a list of tuple which has two lists based on the second list
I have a sequence is like this, seq = [[[“A”,”AA”,”AB”],[0,1,2,3]], [[“B”,”BB”,”BC”],[1,2,3]], [[“C”,”CA”,”CB”],[0,1,2,3]]] I wanted to convert this to something like below [[[‘A’, ‘AA’, ‘AB’, ‘C’, ‘CA’, ‘CB’], [0, 1, 2, 3]], [[‘B’, ‘BB’, ‘BC’], [1, 2, 3]]] I tried but I am getting like below. Can someone help in achieving the correct results. Answer
Pandas Cumcount() over multiple columns
I have a dataframe that looks like this: What I want to do is to add two columns, one for set number and one for rep number. Set number should increase by 1 for each weight change exercise & session is the same, else reset to 0. Rep number should increase by 1 for each velocity change if exercise, session