multi-index

How to insert a row into a multiindex dataframe so that rows sum to the total?

How to insert a row into a multiindex dataframe so that rows sum to the total? Question: I have a multi index dataframe that I want to insert a row into. >>>import numpy as np >>>import pandas as pd >>> date = pd.date_range(‘2023-01-01’, periods=3) >>> size = [‘s’,’m’,’total’] >>> arrays = [date, size] >>> index …

Total answers: 1

Slice MultiIndex by multiple tuples

Slice MultiIndex by multiple tuples Question: I have a DataFrame with multiple index levels. I define some subset by selecting multiple combinations of all levels but the last. Then I want to slice the original DataFrame with that subset, but I cannot find how. Best is to look at a simple example: In [1]: import …

Total answers: 1

Boolean filtering of different lengths with Multi Indexes

Boolean filtering of different lengths with Multi Indexes Question: I am trying to filter my multi-indexed data df based on the column value employ_start_date and index employ_class . data = pd.DataFrame({‘agcy_nbr’: {0: 166, 1: 435, 2: 129, 3: 129, 4: 129}, ’employ_class’: {0: ‘Corr’, 1: ‘LE’, 2: ‘Corr’, 3: ‘Corr’, 4: ‘Corr’}, ’employ_start_date’: {0: 1999, …

Total answers: 1

How to reindex a datetime-based multiindex in pandas

How to reindex a datetime-based multiindex in pandas Question: I have a dataframe that counts the number of times an event has occured per user per day. Users may have 0 events per day and (since the table is an aggregate from a raw event log) rows with 0 events are missing from the dataframe. …

Total answers: 1

Pandas / Matplotlib bar plot with multi index dataframe

Pandas / Matplotlib bar plot with multi index dataframe Question: I have a sorted Multi-Index pandas data frame, which I need to plot in a bar chart. My data frame. I either didn’t find the solution yet, or the simple one doesn’t exist, but I need to plot a bar chart on this data with …

Total answers: 2

How to set pandas multiindex?

How to set pandas multiindex? Question: I have a data frame df which looks like this: year date Time observation1 observation2 2012 11-02 9:12:00 79.373668 224 2012 11-02 9:13:00 130.841316 477 2012 11-05 9:14:00 45.312814 835 2013 11-05 9:15:00 123.776946 623 2013 11-05 9:16:00 79.373668 224 2013 11-22 9:17:00 130.841316 477 2013 11-22 9:18:00 45.312814 …

Total answers: 1

Don't make a second level when aggregating in Pandas

Don't make a second level when aggregating in Pandas Question: Indexes and levels in Pandas still drive me nuts when using it. My dataframe structure looks like that: Indexstruktur Datensatz Workload Selectivity Dimensionen Anzahl Datenpunkte Operation Measure Wert I now group by all columns except the last one ("Wert"). This one will be used to …

Total answers: 1

Vectorized column creation, where each value is pulled from a different column in a multi-index pandas df

Vectorized column creation, where each value is pulled from a different column in a multi-index pandas df Question: I have a multiindex dataframe like the one below: import numpy as np import pandas as pd np.random.seed(1) df = pd.DataFrame( { ‘trial’: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2], ‘t’: [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,0,0,0,0,1,1,1,1], ‘context’:[0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3], ‘x’ :np.random.rand(40), ‘y’ : np.random.rand(40), ‘z’ : np.random.rand(40), ‘inferred_context’: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1], …

Total answers: 1

python pandas: using slice to build a multiindex slicing in pandas

python pandas: using slice to build a multiindex slicing in pandas Question: I have a double Multiindex dataframe as follows. I slice the rows with idx = pd.IndexSlice but I dont know how to do the same with the columns so provided this data: df = pd.DataFrame(data=pd.DataFrame(data=np.random.randint(0, 10, size=(9, 5)))) # rows list1 = [‘2021-01-01′,’2022-02-01′,’2022-03-01’] …

Total answers: 1