Python pandas groupby aggregate on multiple columns, then pivot

df.groupby(‘Category’).agg({‘Item’:’size’,’shop1′:[‘sum’,’mean’,’std’],’shop2′:[‘sum’,’mean’,’std’],’shop3′:[‘sum’,’mean’,’std’]}) Or if you want it across all shops then: df1 = df.set_index([‘Item’,’Category’]).stack().reset_index().rename(columns={‘level_2′:’Shops’,0:’costs’}) df1.groupby(‘Category’).agg({‘Item’:’size’,’costs’:[‘sum’,’mean’,’std’]})

Deleting specific rows with a pattern[start and end indicators] from a dataframe [closed]

Using a toy example… df <- data.frame(a=LETTERS[1:10],b=LETTERS[3:12],stringsAsFactors = FALSE) limits <- c(“E”,”H”) sapply(df,function(x){ del.min <- grep(limits[1],x) del.max <- grep(limits[2],x) x[del.min:del.max] <- “” return(x)}) a b [1,] “A” “C” [2,] “B” “D” [3,] “C” “” [4,] “D” “” [5,] “” “” [6,] “” “” [7,] “” “I” [8,] “” “J” [9,] “I” “K” [10,] “J” “L”