How to access pandas groupby dataframe by key

How do I access the corresponding groupby dataframe in a groupby object by the key? With the following groupby: rand = np.random.RandomState(1) df = pd.DataFrame({‘A’: [‘foo’, ‘bar’] * 3, ‘B’: rand.randn(6), ‘C’: rand.randint(0, 20, 6)}) gb = df.groupby([‘A’]) I can iterate through it to get the keys and groups: In [11]: for k, gp in … Read more

How to group time by hour or by 10 minutes

like when I do SELECT [Date] FROM [FRIIB].[dbo].[ArchiveAnalog] GROUP BY [Date] how can I specify the group period ? MS SQL 2008 2nd Edit I’m trying SELECT MIN([Date]) AS RecT, AVG(Value) FROM [FRIIB].[dbo].[ArchiveAnalog] GROUP BY (DATEPART(MINUTE, [Date]) / 10) ORDER BY RecT changed %10 to / 10. is it possible to make Date output without … Read more

Relative frequencies / proportions with dplyr

Suppose I want to calculate the proportion of different values within each group. For example, using the mtcars data, how do I calculate the relative frequency of number of gears by am (automatic/manual) in one go with dplyr? library(dplyr) data(mtcars) mtcars <- tbl_df(mtcars) # count frequency mtcars %>% group_by(am, gear) %>% summarise(n = n()) # … Read more

Pandas dataframe get first row of each group

I have a pandas DataFrame like following: df = pd.DataFrame({‘id’ : [1,1,1,2,2,3,3,3,3,4,4,5,6,6,6,7,7], ‘value’ : [“first”,”second”,”second”,”first”, “second”,”first”,”third”,”fourth”, “fifth”,”second”,”fifth”,”first”, “first”,”second”,”third”,”fourth”,”fifth”]}) I want to group this by [“id”,”value”] and get the first row of each group: id value 0 1 first 1 1 second 2 1 second 3 2 first 4 2 second 5 3 first 6 3 … Read more

pandas GroupBy columns with NaN (missing) values

I have a DataFrame with many missing values in columns which I wish to groupby: import pandas as pd import numpy as np df = pd.DataFrame({‘a’: [‘1’, ‘2’, ‘3’], ‘b’: [‘4’, np.NaN, ‘6’]}) In [4]: df.groupby(‘b’).groups Out[4]: {‘4’: [0], ‘6’: [2]} see that Pandas has dropped the rows with NaN target values. (I want to … Read more