Laravel attach pivot to table with multiple values

You can. From this example in Docs (4.2, 5.0): $user->roles()->sync(array(1 => array(‘expires’ => true))); Hardcoded version for the first two rows: $food = Food::find(1); $food->allergies()->sync([1 => [‘severity’ => 3], 4 => [‘severity’ => 1]]); Dynamically, with your arrays $allergy_ids and $severities in a compatible state (size and sort), you shall prepare your sync data before. … Read more

Convert matrix to 3-column table (‘reverse pivot’, ‘unpivot’, ‘flatten’, ‘normalize’)

To “reverse pivot”, “unpivot” or “flatten”: For Excel 2003: Activate any cell in your summary table and choose Data – PivotTable and PivotChart Report: For later versions access the Wizard with Alt+D, P. For Excel for Mac 2011, it’s ⌘+Alt+P (See here). Select Multiple consolidation ranges and click Next. In “Step 2a of 3”, choose … Read more

Difference between groupby and pivot_table for pandas dataframes

Both pivot_table and groupby are used to aggregate your dataframe. The difference is only with regard to the shape of the result. Using pd.pivot_table(df, index=[“a”], columns=[“b”], values=[“c”], aggfunc=np.sum) a table is created where a is on the row axis, b is on the column axis, and the values are the sum of c. Example: df … Read more

How to get rid of multilevel index after using pivot table pandas?

You need remove only index name, use rename_axis (new in pandas 0.18.0): print (reshaped_df) sale_product_id 1 8 52 312 315 sale_user_id 1 1 1 1 5 1 print (reshaped_df.index.name) sale_user_id print (reshaped_df.rename_axis(None)) sale_product_id 1 8 52 312 315 1 1 1 1 5 1 Another solution working in pandas below 0.18.0: reshaped_df.index.name = None print … Read more

Pandas Pivot tables row subtotals

If you put State and City not both in the rows, you’ll get separate margins. Reshape and you get the table you’re after: In [10]: table = pivot_table(df, values=[‘SalesToday’, ‘SalesMTD’,’SalesYTD’],\ rows=[‘State’], cols=[‘City’], aggfunc=np.sum, margins=True) In [11]: table.stack(‘City’) Out[11]: SalesMTD SalesToday SalesYTD State City stA All 900 50 2100 ctA 400 20 1000 ctB 500 30 … Read more

How is a pivot table created by Laravel?

It appears as though the pivot table does need to be created manually (i.e. Laravel does not do this automatically). Here’s how to do it: 1.) Create a new migration, using singular table names in alphabetical order (default): php artisan make:migration create_alpha_beta_table –create –table=alpha_beta 2.) Inside the newly created migration, change the up function to: … Read more

Difference between pivot and pivot_table. Why is only pivot_table working?

For anyone who is still interested in the difference between pivot and pivot_table, there are mainly two differences: pivot_table is a generalization of pivot that can handle duplicate values for one pivoted index/column pair. Specifically, you can give pivot_table a list of aggregation functions using keyword argument aggfunc. The default aggfunc of pivot_table is numpy.mean. … Read more