Pivot table subtotals in Pandas

your pivot table table = pd.pivot_table(df, values=[‘Amount’], index=[‘Location’, ‘Employee’], columns=[‘Account’, ‘Currency’], fill_value=0, aggfunc=np.sum, dropna=True, ) print(table) Amount Account Basic Net Currency GBP USD GBP USD Location Employee Airport Test 2 0 3000 0 2000 Town Test 1 0 4000 0 3000 Test 3 5000 0 4000 0 pandas.concat pd.concat([ d.append(d.sum().rename((k, ‘Total’))) for k, d in … Read more

Rotate – Transposing a List using LINQ C#

This is a simple and flexible solution, it will handle multiple inner lists with any number of dimensions. List<List<string>> PersonInfo = new List<List<string>>() { new List<string>() {“John”, “Peter”, “Watson”}, new List<string>() {“1000”, “1001”, “1002”} }; var result = PersonInfo .SelectMany(inner => inner.Select((item, index) => new { item, index })) .GroupBy(i => i.index, i => i.item) … Read more

Pivoting a Pandas Dataframe containing strings – ‘No numeric types to aggregate’ error

The default aggfunc in pivot_table is np.sum and it doesn’t know what to do with strings and you haven’t indicated what the index should be properly. Trying something like: pivot_table = unified_df.pivot_table(index=[‘id’, ‘contact_id’], columns=”question”, values=”response_answer”, aggfunc=lambda x: ‘ ‘.join(x)) This explicitly sets one row per id, contact_id pair and pivots the set of response_answer values … Read more

MySQL dynamic pivot table

Since the values are in int you are are making them the column names, you have to wrap the values in a backtick The sql will look like: max(case when user_id = 1 then score end) as `1` The full query will be: SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( ‘max(case when user_id = ”’, … Read more