aggregate

Group by two columns where values may be switched

Group by two columns where values may be switched Question: I have a dataframe that looks like this: Start End Total X Y 50 X Z 100 Y Z 50 Y X 100 I am trying to group by pairs "Start" –> "End", where values may be switched. I’m calculating the sum of "Total". The …

Total answers: 1

Applying Groupby and aggregation on a set of dynamically selected columns

Applying Groupby and aggregation on a set of dynamically selected columns Question: I have a dataframe and need to group the data based on some columns. Static way: dfMMS_BBMS_pv.columns [‘area_type’, ‘area_name’, ‘area_code’, ‘date’, ‘A_BBMS’, ‘A_MMS’, ‘Others_BBMS’, ‘Others_MMS’, ‘B_BBMS’, ‘C_BBMS’, ‘C_MMS’, ‘T_BBMS’, ‘V_BBMS’, ‘D_BBMS’, ‘D_MMS’] dfMMS_BBMS_pv=dfMMS_BBMS_pv.groupby([‘area_type’, ‘area_name’, ‘area_code’]). agg({‘date’: lambda x: list((x)) ,’A_MMS’: lambda x: list(round(x,2)) …

Total answers: 1

Pandas aggregate two columns at max

Pandas aggregate two columns at max Question: I have a data frame with two columns df = DataFrame.from_records([ {"time": 10, "amount": 200}, {"time": 70, "amount": 1000}, {"time": 10, "amount": 300}, {"time": 10, "amount": 100}, ]) I want to, given a period of time 80ms, calculate the max amount that is possible, in this case, the …

Total answers: 2

pandas default aggregation function for rest of the columns

pandas default aggregation function for rest of the columns Question: I’d need to groupby and aggregate dataframe. Some columns have specific aggregation function, for the rest I’d like to use first. I just don’t want to hardcode the rest of column names, because it can differ by case. Do you have any elegant idea how …

Total answers: 2

Pandas resampling data with bigger interval than a whole index range

Pandas resampling data with bigger interval than a whole index range Question: Situation I have the folowwing pandas timeseries data: date predicted1 2001-03-13 0.994756 2005-08-22 0.551661 2000-05-07 0.001396 I need to take into account a case of resampling into bigger interval than a 5 years, for e.g. 10 years: sample = data.set_index(pd.DatetimeIndex(data[‘date’])).drop(‘date’, axis=1)[‘predicted1′] sample.resample(’10Y’).sum() I …

Total answers: 1

Split Column of Semicolon-Separated Values and Duplicate Row with each Value in Pandas

Split Column of Semicolon-Separated Values and Duplicate Row with each Value in Pandas Question: I have a data frame with a row of data like this: play_by_play = pd.DataFrame([{ "players": "Tom Brady; Mike Evans; Tristan Wirfs; Leonard Fournette; Chris Godwin", "down": 1, "to_go": 10, "play_type": ‘pass’, "yards_gained": 8, "pass_attempt": 1, "complete_pass": 1, "rush_attempt": 0 }]) …

Total answers: 1

Pandas aggregating groupby object using multiple conditions in other columns

Pandas aggregating groupby object using multiple conditions in other columns Question: In my project, I have a dataframe that goes a little something like this: df = pd.DataFrame([ {‘posteam’: ‘NYJ’, ‘defteam’: ‘BAL’, ‘penalty’: 1, ‘penalty_team’: ‘NYJ’, ‘penalty_yards’: 10}, {‘posteam’: ‘NYJ’, ‘defteam’: ‘BAL’, ‘penalty’: 1, ‘penalty_team’: ‘BAL’, ‘penalty_yards’: 5}, {‘posteam’: ‘BAL’, ‘defteam’: ‘NYJ’, ‘penalty’: 0, ‘penalty_team’: …

Total answers: 1

Group by column in Pandas and count Unique values in each group

Group by column in Pandas and count Unique values in each group Question: I’m trying to use groupby in pandas to group by a variable column and count the number of times a value shows up in the each group. For example, using this group: d = {‘Period’: [1, 2, 3, 4, 1, 2, 3, …

Total answers: 2