grouping table elements according to grouping column values
By : Bill Anderson
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have this table, with number of photon counts of an event in each energy channel. , Give this a try, code :
chan = np.array( [0,1,2,3,4,5,6,7,8,9] )
counts = np.array( [0.,0.,5.,2.,0.,0.,1.,1.,1.,0.] )
groups = np.array( [1,1,1,1,1,1,1,1,1,1] )
indx = np.where( groups==1 )
# indx is a tuple with one entry for each dimension of the array groups
# in the next statement I just grab the first (and only) element of the tuple
indx = indx[0]
# next we split the array based on those indices
counts = np.split( counts, indx )
# counts is now a list of arrays
# [array([], dtype=float64), array([ 0., 0., 5., 0., 0.]),array([ 0., 1., 1., 1., 0.])]
# I use the if statement in the list comprehension to get rid of the first empty array
totals = np.array( [sum(c) for c in counts if len(c)>0] )
tchnls = np.split( chan, indx )[1:]
>>> totals
array([ 7., 3.])
>>> tchnls
[array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]

How to find highest values in multiple columns by grouping the row values using python?
By : msgoon6
Date : March 29 2020, 07:55 AM
I hope this helps you . I am working on a dataset that contains three columns; roadType (string), cars and buses (integer values). , try this: code :
In [31]: d = pd.DataFrame(data, columns=['roadType','cars','buses'])
In [32]: d
Out[32]:
roadType cars buses
0 A 5 6
1 B 7 3
2 C 9 6
3 B 2 8
4 A 4 8
5 C 8 1
6 B 1 0
In [33]: d.groupby('roadType').max().reset_index()
Out[33]:
roadType cars buses
0 A 5 8
1 B 7 8
2 C 9 6

Find the average values by grouping and subgrouping variables, and count of the number of times a value occurs within t
By : Rameshwary
Date : March 29 2020, 07:55 AM
With these it helps I have a dataset which has four columns of data. , We don't need to reshape it to 'long' format to get the mean values code :
library(dplyr)
df %>%
group_by(Grouping) %>% #first grouping
#create the mean column and the count by 'Grouping'
mutate(AvgPrice = mean(Price), n1 = n()) %>%
group_by(Sub_grouping, add= TRUE) %>% #second grouping
#summarise to get the mean within Sub_grouping and count the values with n()
summarise(AvgPrice = first(AvgPrice), n1 = first(n1), AvgPrice2 = mean(Price), n2 = n())
df
%>%
group_by(Grouping, SP) %>%
...
...
library(data.table)
dcast(setDT(df), Grouping + Sub_grouping ~ SP, value.var = "Price", c(mean, length))

From a Pandas Dataframe, return specific column values based on grouping and largest values of other columns
By : نهجي علي الكرار
Date : March 29 2020, 07:55 AM
hope this fix your issue IIUC, you can sort_values, then apply head to your groupby object code :
df_new = df.sort_values(['Class', 'Grade'], ascending=[True, False]).groupby('Class').head(2)
Class Grade Student
0 A 96 Eric
1 A 92 Caden
4 B 80 Laura
5 B 22 Leann
7 C 90 Jack
8 C 87 Jill
10 D 48 Andrew
15 E 93 Sarah
16 E 92 Brit
df_new[['Student']]
Student
0 Eric
1 Caden
4 Laura
5 Leann
7 Jack
8 Jill
10 Andrew
15 Sarah
16 Brit

Replicate values across a grouping set with multiple groups within the grouping set by date
By : user3257125
Date : March 29 2020, 07:55 AM
To fix this issue Without ROWS your current query returns the Max over all rows, i.e. a Group Max. You probably want code :
LAST_VALUE(GR_MATERIAL IGNORE NULLS)
over (partition by A_ID, B_ID, C_ID
order by LAST_DT DESC, DATE_TIME DESC)

