Questions tagged [multi-index]

A multi-index (also known as a hierarchical index) allows the manipulation of higher-dimensional data in a 2-dimensional tabular structure.

A multi-index exists when an indexing column (or row) of a tabular structure contains more than one line. The use of layered indexing enables one to effectively store and manipulate arbitrarily high dimension data in a 2-dimensional tabular structure 1. This is also called "hierarchical" or "multi-level" indexing.

2771 questions
1
vote
1 answer

How to add a new row to pandas dataframe with non-unique multi-index

df = pd.DataFrame(np.arange(4*3).reshape(4,3), index=[['a','a','b','b'],[1,2,1,2]], columns=list('xyz')) where df looks like: Now I add a new row by: df.loc['new',:]=[0,0,0] Then df becomes: Now I want to do the same but with a different df that…
Royalblue
  • 639
  • 10
  • 22
1
vote
1 answer

Remove index from MultiIndex dataframe if child index has column value meeting criteria

I had originally asked this question here, and I believe it was incorrectly marked as a duplicate. I will do my best here to clarify my question and how I believe it is unique. Given the following example MultiIndex dataframe: import pandas as…
rahlf23
  • 8,869
  • 4
  • 24
  • 54
1
vote
2 answers

Adding an Average Column to a Pandas Multiindex Dataframe

I have a dataframe df first bar baz second one two one two A 0.487880 -0.487661 -1.030176 0.100813 B 0.267913 1.918923 0.132791 0.178503 C 1.550526 -0.312235 -1.177689…
rhaskett
  • 1,864
  • 3
  • 29
  • 48
1
vote
1 answer

Use columns from one DataFrame as MultiIndex for t-test in another

What is the best practice for using the columns from one DataFrame as the indexes into another MultiIndexed DataFrame using Pandas to run a t-test? I've seen a couple other similar questions that involved looping on here that don't seem like they…
Rorschach
  • 31,301
  • 5
  • 78
  • 129
1
vote
0 answers

wicked pandas pivot with multi-index, regex

i've tried every combination of pandas.melt and .stack and .pivot but have not made any progress. i have an excel table in the following general format: 1997 1998 1999 2000 Total, N (%) 3350 (34) 3387…
user426
  • 31
  • 4
1
vote
1 answer

Select according to first two levels of multi-index

Take the following code: df = pd.DataFrame([range(5),range(5),range(5),range(5)], columns=range('a','e')) df.set_index(['a','b','c','d']) subselection = df[0, 2] The second line of code is supposed to fetch the sub-dataframe according to the first…
wlad
  • 2,073
  • 2
  • 18
  • 29
1
vote
2 answers

Pandas Return top N from each group in MultiIndex

I have a large multi-index dataframe with about 3 million data points. The first index is the date, the second is the name of the object. Then, each object has a value like so: df = pd.DataFrame({'year': [2012, 2012, 2012, 2013, 2013,…
mdawg
  • 49
  • 5
1
vote
1 answer

Add column using groupby in multiindex Pandas

I am trying to find the sum of a column based upon the groupby function. so in this example i want to find the sum of all the bar, baz, foo, and qux. the sum would be added to a new column at the end. i can get the results i need but i can not…
1
vote
2 answers

get index from subset of pandas multindex

Given this pandas.core.frame.DataFrame sample: col 0 2 0 0 1 3 1 I would like to get those indexes where col is 1: df[df['col']==1] The expected result would be a vector (0,3).
PeCaDe
  • 277
  • 1
  • 8
  • 33
1
vote
1 answer

Multiindex categorization and encoding this in PySpark

I have multiindex mapping rules, here's the rules Type A: Chicken, Beef, Goat Type B: Fish, Shrimp Type C: Chicken, Pork I here's my dataframe, let say this is a df dataframe, and want to do multi index mapping id Menu 1 Fried Chicken …
Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70
1
vote
1 answer

Transforming a multi-index dataframe to retain a groupby() aggregation

Issue: My dataframe has multiple columns containing the information of certain products grouped by brands. Every product has some attributes represented in column 1, column 2 and so forth. These columns each capture additional information in the…
nid
  • 155
  • 3
  • 9
1
vote
1 answer

Multi-indexing - pandas

I'm trying to build my own stocks screener with pandas. I fetch data with the DataReader, from Morningstar. Here is my data structure : 'Close', 'Volume', 'Low', 'High' etc are columns; 'Date' are rows. I could be wrong, but I have chosen to index…
citizen007
  • 11
  • 2
1
vote
2 answers

resample a MultiIndex

I have a DataFrame with a MultiIndex. The first level is a DatetimeIndex with weekly frequency. The second level is NOT consistent across groupings by the first level. I want to group the first level by month and take the first weeks…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
1
vote
1 answer

Inconsistent behaviour in MultiIndex Indexing

I am getting inconsistent indexing behaviour depending on whether one of the indices in my MultiIndex dataframe is an integer or a string. Here is an example: MultiIndex with one string index: a =…
Sansport
  • 27
  • 1
  • 5
1
vote
3 answers

Pandas MultiIndex Vector Setting

I have a DataFrame with multiindex like this: 0 1 2 a 0 0.928295 0.828225 -0.612509 1 1.103340 -0.540640 -0.344500 2 -1.760918 -1.426488 -0.647610 3 -0.782976 0.359211 1.601602 4 0.334406 -0.508752…
Kid
  • 413
  • 4
  • 11
1 2 3
99
100