group-by

Apply different functions to different columns with a singe pandas groupby command

╄→尐↘猪︶ㄣ 提交于 2020-04-30 10:57:37
问题 My data is stored in df . I have multiple users per group . I want to group df by group and apply different functions to different columns. The twist is that I would like to assign custom names to the new columns during this process. np.random.seed(123) df = pd.DataFrame({"user":range(4),"group":[1,1,2,2],"crop":["2018-01-01","2018-01-01","2018-03-01","2018-03-01"], "score":np.random.randint(400,1000,4)}) df["crop"] = pd.to_datetime(df["crop"]) print(df) user group crop score 0 0 1 2018-01-01

mongodb $group compare one collection to other collection

こ雲淡風輕ζ 提交于 2020-04-30 06:24:50
问题 I got stuck in one point. See this playground, it should follow this logic: if history's collection is empty, return data if history's date collection is greater then main's collection base on specified user_id , return nothing if specified user_id doesn't match with history's collection date then it should return all data that is in main collection. So everything looks fine in playground the only problem when i am putting 5e4e74eb380054797d9db623 id it should not return me data because its

How to remove some rows in a group by in python

送分小仙女□ 提交于 2020-04-18 09:55:07
问题 I'm having a dataframe and I'd like to do a groupby() based a column and then sort the values within each group based on a date column. Then, from a each I'd like to remove records whose value for column_condition == 'B' until I reach to a row whose column_condition == 'A' . For example, Assume the table below is one of the groups ID, DATE, column_condition -------------------------- 1, jan 2017, B 1, Feb 2017, B 1, Mar 2017, B 1, Aug 2017, A 1, Sept 2017, B So, I'd like to remove the first

Take maximum rainfall value for each season over a time period (xarray)

…衆ロ難τιáo~ 提交于 2020-04-18 06:08:42
问题 I'm trying to find the maximum rainfall value for each season (DJF, MAM, JJA, SON) over a 10 year period. I am using netcdf data and xarray to try and do this. The data consists of rainfall (recorded every 3 hours), lat, and lon data. Right now I have the following code: ds.groupby('time.season).max('time') However, when I do it this way the output has a shape of (4,145,192) indicating that it's taking the maximum value for each season over the entire period. I would like the maximum for each

Include missing group keys as NaN in pandas GroupBy output

杀马特。学长 韩版系。学妹 提交于 2020-04-14 15:50:51
问题 I have a dataframe in pandas. test_df = pd.DataFrame({'date': ['2018-12-28', '2018-12-28', '2018-12-29', '2018-12-29', '2018-12-30', '2018-12-30'], 'transaction': ['aa', 'bb', 'cc', 'aa', 'bb', 'bb'], 'ccy': ['USD', 'EUR', 'EUR', 'USD', 'USD', 'USD'], 'amt': np.random.random(6)}) test_df: date transaction ccy amt 2018-12-28 aa USD 0.323439 2018-12-28 bb EUR 0.048948 2018-12-29 cc EUR 0.793263 2018-12-29 aa USD 0.013865 2018-12-30 bb USD 0.658571 2018-12-30 bb USD 0.224951 The following code

Include missing group keys as NaN in pandas GroupBy output

情到浓时终转凉″ 提交于 2020-04-14 15:45:29
问题 I have a dataframe in pandas. test_df = pd.DataFrame({'date': ['2018-12-28', '2018-12-28', '2018-12-29', '2018-12-29', '2018-12-30', '2018-12-30'], 'transaction': ['aa', 'bb', 'cc', 'aa', 'bb', 'bb'], 'ccy': ['USD', 'EUR', 'EUR', 'USD', 'USD', 'USD'], 'amt': np.random.random(6)}) test_df: date transaction ccy amt 2018-12-28 aa USD 0.323439 2018-12-28 bb EUR 0.048948 2018-12-29 cc EUR 0.793263 2018-12-29 aa USD 0.013865 2018-12-30 bb USD 0.658571 2018-12-30 bb USD 0.224951 The following code

Pandas assign the groupby sum value to the last row in the original table

久未见 提交于 2020-04-13 06:47:23
问题 For example, I have a table A id price sum 1 2 0 1 6 0 1 4 0 2 2 0 2 10 0 2 1 0 2 5 0 3 1 0 3 5 0 What I want is like (the last row of sum should be the sum of price of a group) id price sum 1 2 0 1 6 0 1 4 12 2 2 0 2 10 0 2 1 0 2 5 18 3 1 0 3 5 6 What I can do is find out the sum using A['price'].groupby(A['id']).transform('sum') However I don't know how to assign this to the sum column (last row). Thanks 回答1: Use last_valid_index to locate rows to fill g = df.groupby('id') l = pd.DataFrame

how to group array of objects by value in reactjs / javascript

社会主义新天地 提交于 2020-04-10 05:27:13
问题 I have an array of object that have different values like items=[{id:1,category:"cat_1" , title:"My title 1"},{id:2,category:"cat_2" , title:"My title 2"},{id:6,category:"cat_1" , title:"Another title 1"},{id:1,category:"cat_3" , title:"My title 3"},{id:8,category:"cat_1" , title:"Third Title"},{id:2,category:"cat_2" , title:"Another title 2 "}] I use array map to list the object and display them as { items.map((item) => ( <h1>{item.category}</h1> <p>{item.title}</p> ))} My question is how do

LINQ GroupBy with a dynamic group of columns

孤街浪徒 提交于 2020-04-09 22:00:53
问题 I have a table like this: variety category quantity ---------------------------------------------- rg pm 10 gs pm 5 rg com 8 I want to make a GroupBy based on these bool parameters: IncludeVariety IncludeCategory eg: IncludeVariety = true; IncludeCategory = true; would return this: variety category quantity ---------------------------------------------- rg pm 10 gs pm 5 rg com 8 and this: IncludeVariety = true; IncludeCategory = false; would return this: variety category quantity ------------

LINQ GroupBy with a dynamic group of columns

送分小仙女□ 提交于 2020-04-09 21:58:49
问题 I have a table like this: variety category quantity ---------------------------------------------- rg pm 10 gs pm 5 rg com 8 I want to make a GroupBy based on these bool parameters: IncludeVariety IncludeCategory eg: IncludeVariety = true; IncludeCategory = true; would return this: variety category quantity ---------------------------------------------- rg pm 10 gs pm 5 rg com 8 and this: IncludeVariety = true; IncludeCategory = false; would return this: variety category quantity ------------