multi-index

Concatenate Pandas columns under new multi-index level

房东的猫 提交于 2019-11-26 17:58:04
问题 Given a dictionary of data frames like: dict = {'ABC': df1, 'XYZ' : df2} # of any length... where each data frame has the same columns and similar index, for example: data Open High Low Close Volume Date 2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833 2002-01-18 0.18439 0.21331 0.18077 0.19523 2027866 2002-01-21 0.19523 0.20970 0.19162 0.20608 771149 What is the simplest way to combine all the data frames into one, with a multi-index like: symbol ABC XYZ data Open High Low Close Volume

pandas dataframe select columns in multiindex [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-26 17:56:50
问题 This question already has an answer here: Selecting columns from pandas MultiIndex 7 answers I have the following pd.DataFrame: Name 0 1 ... Col A B A B ... 0 0.409511 -0.537108 -0.355529 0.212134 ... 1 -0.332276 -1.087013 0.083684 0.529002 ... 2 1.138159 -0.327212 0.570834 2.337718 ... It has MultiIndex columns with names=['Name', 'Col'] and hierarchical levels. The Name label goes from 0 to n, and for each label, there are two A and B columns. I would like to subselect all the A (or B )

selecting from multi-index pandas

元气小坏坏 提交于 2019-11-26 15:13:58
问题 I have a multi-index data frame with columns 'A' and 'B'. Is there is a way to select rows by filtering on one column of the multi-index without resetting the index to a single column index? For Example. # has multi-index (A,B) df #can I do this? I know this doesn't work because the index is multi-index so I need to specify a tuple df.ix[df.A ==1] 回答1: One way is to use the get_level_values Index method: In [11]: df Out[11]: 0 A B 1 4 1 2 5 2 3 6 3 In [12]: df.iloc[df.index.get_level_values(

Nested dictionary to multiindex dataframe where dictionary keys are column labels

你离开我真会死。 提交于 2019-11-26 12:22:28
问题 Say I have a dictionary that looks like this: dictionary = {\'A\' : {\'a\': [1,2,3,4,5], \'b\': [6,7,8,9,1]}, \'B\' : {\'a\': [2,3,4,5,6], \'b\': [7,8,9,1,2]}} and I want a dataframe that looks something like this: A B a b a b 0 1 6 2 7 1 2 7 3 8 2 3 8 4 9 3 4 9 5 1 4 5 1 6 2 Is there a convenient way to do this? If I try: In [99]: DataFrame(dictionary) Out[99]: A B a [1, 2, 3, 4, 5] [2, 3, 4, 5, 6] b [6, 7, 8, 9, 1] [7, 8, 9, 1, 2] I get a dataframe where each element is a list. What I need

Pandas dataframe with multiindex column - merge levels

大城市里の小女人 提交于 2019-11-26 12:09:10
问题 I have a dataframe, grouped , with multiindex columns as below: import pandas as pd codes = [\"one\",\"two\",\"three\"]; colours = [\"black\", \"white\"]; textures = [\"soft\", \"hard\"]; N= 100 # length of the dataframe df = pd.DataFrame({ \'id\' : range(1,N+1), \'weeks_elapsed\' : [random.choice(range(1,25)) for i in range(1,N+1)], \'code\' : [random.choice(codes) for i in range(1,N+1)], \'colour\': [random.choice(colours) for i in range(1,N+1)], \'texture\': [random.choice(textures) for i

Benefits of panda's multiindex?

試著忘記壹切 提交于 2019-11-26 10:23:29
问题 So I learned that I can use DataFrame.groupby without having a MultiIndex to do subsampling/cross-sections. On the other hand, when I have a MultiIndex on a DataFrame, I still need to use DataFrame.groupby to do sub-sampling/cross-sections. So what is a MultiIndex good for apart from the quite helpful and pretty display of the hierarchies when printing? 回答1: Hierarchical indexing (also referred to as “multi-level” indexing) was introduced in the pandas 0.4 release. This opens the door to some

Pandas: add a column to a multiindex column dataframe

China☆狼群 提交于 2019-11-26 07:25:21
问题 I would like to add a column to the second level of a multiindex column dataframe. In [151]: df Out[151]: 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 -0.081596 The usual trick of direct assignment does not work: In [152]: df[\'bar\'][\'three\'] = [0, 1, 2] In [153]: df Out[153]: 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

How to query MultiIndex index columns values in pandas

半世苍凉 提交于 2019-11-26 07:19:07
问题 Code example: In [171]: A = np.array([1.1, 1.1, 3.3, 3.3, 5.5, 6.6]) In [172]: B = np.array([111, 222, 222, 333, 333, 777]) In [173]: C = randint(10, 99, 6) In [174]: df = pd.DataFrame(zip(A, B, C), columns=[\'A\', \'B\', \'C\']) In [175]: df.set_index([\'A\', \'B\'], inplace=True) In [176]: df Out[176]: C A B 1.1 111 20 222 31 3.3 222 24 333 65 5.5 333 22 6.6 777 74 Now, I want to retrieve A values: Q1 : in range [3.3, 6.6] - expected return value: [3.3, 5.5, 6.6] or [3.3, 3.3, 5.5, 6.6] in

How to move pandas data from index to column after multiple groupby

狂风中的少年 提交于 2019-11-26 06:37:53
问题 I have the following pandas dataframe: dfalph.head() token year uses books 386 xanthos 1830 3 3 387 xanthos 1840 1 1 388 xanthos 1840 2 2 389 xanthos 1868 2 2 390 xanthos 1875 1 1 I aggregate the rows with duplicate token and years like so: dfalph = dfalph[[\'token\',\'year\',\'uses\',\'books\']].groupby([\'token\', \'year\']).agg([np.sum]) dfalph.columns = dfalph.columns.droplevel(1) dfalph.head() uses books token year xanthos 1830 3 3 1840 3 3 1867 2 2 1868 2 2 1875 1 1 Instead of having

Turn Pandas Multi-Index into column

吃可爱长大的小学妹 提交于 2019-11-26 06:17:15
问题 I have a dataframe with 2 index levels: value Trial measurement 1 0 13 1 3 2 4 2 0 NaN 1 12 3 0 34 Which I want to turn into this: Trial measurement value 1 0 13 1 1 3 1 2 4 2 0 NaN 2 1 12 3 0 34 How can I best do this? I need this because I want to aggregate the data as instructed here, but I can\'t select my columns like that if they are in use as indices. 回答1: The reset_index() is a pandas DataFrame method that will transfer index values into the DataFrame as columns. The default setting