multi-index

Concatenate Pandas columns under new multi-index level

笑着哭i 提交于 2019-11-27 10:47:54
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 Open ... Date 2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833 ... 2002-01-18 0.18439 0.21331 0.18077 0

pandas dataframe select columns in multiindex [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-11-27 10:29:42
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 ) columns of this DataFrame. There is a get_level_values method that you can use in conjunction with boolean

Pandas Plotting with Multi-Index

烂漫一生 提交于 2019-11-27 09:47:11
问题 After performing a groupby.sum() on a DataFrame I'm having some trouble trying to create my intended plot. How can I create a subplot ( kind='bar' ) for each Code , where the x-axis is the Month and the bars are ColA and ColB ? 回答1: I found the unstack(level) method to work perfectly, which has the added benefit of not needing a priori knowledge about how many Codes there are. df.unstack(level=0).plot(kind='bar', subplots=True) 回答2: Using the following DataFrame ... # using pandas version 0

pandas: how to run a pivot with a multi-index?

寵の児 提交于 2019-11-27 09:45:15
问题 I would like to run a pivot on a pandas DataFrame , with the index being two columns, not one. For example, one field for the year, one for the month, an 'item' field which shows 'item 1' and 'item 2' and a 'value' field with numerical values. I want the index to be year + month. The only way I managed to get this to work was to combine the two fields into one, then separate them again. is there a better way? Minimal code copied below. Thanks a lot! PS Yes, I am aware there are other

Multi Index Sorting in Pandas

▼魔方 西西 提交于 2019-11-27 09:38:43
问题 I have a dataset with multi-index columns in a pandas df that I would like to sort by values in a specific column. I have tried using sortindex and sortlevel but haven't been able get the results I am looking for. My dataset looks like: Group1 Group2 A B C A B C 1 1 0 3 2 5 7 2 5 6 9 1 0 0 3 7 0 2 0 3 5 I want to sort all data and the index by column C in Group 1 in descending order so my results look like: Group1 Group2 A B C A B C 2 5 6 9 1 0 0 1 1 0 3 2 5 7 3 7 0 2 0 3 5 Is it possible to

Sum columns by level in a Multi-Index DataFrame

*爱你&永不变心* 提交于 2019-11-27 08:24:28
问题 I have my df with multi-index columns. All of my values are in float, and I want to merge values with in first level of multi-index. Please see below for detail. first bar baz foo second one two one two one A 0.895717 0.805244 1.206412 2.565646 1.431256 B 0.410835 0.813850 0.132003 0.827317 0.076467 C 1.413681 1.607920 1.024180 0.569605 0.875906 first bar baz foo A (0.895717+0.805244) (1.206412+2.565646) 1.431256 B (0.410835+0.813850) (0.132003+0.827317) 0.076467 C (1.413681+1.607920) (1

Pandas: get multiindex level as series

别说谁变了你拦得住时间么 提交于 2019-11-27 07:38:43
问题 I have a dataframe with multiple levels, eg: idx = pd.MultiIndex.from_product((['foo', 'bar'], ['one', 'five', 'three' 'four']), names=['first', 'second']) df = pd.DataFrame({'A': [np.nan, 12, np.nan, 11, 16, 12, 11, np.nan]}, index=idx).dropna().astype(int) A first second foo five 12 four 11 bar one 16 five 12 three 11 I want to create a new column using the index level titled second , so that I get A B first second foo five 12 five four 11 four bar one 16 one five 12 five three 11 three I

a C++ hash map that preserves the order of insertion [duplicate]

只谈情不闲聊 提交于 2019-11-27 06:14:56
问题 This question already has an answer here: A std::map that keep track of the order of insertion? 14 answers I have the following code: #include <iostream> #include "boost/unordered_map.hpp" using namespace std; using namespace boost; int main() { typedef unordered_map<int, int> Map; typedef Map::const_iterator It; Map m; m[11] = 0; m[0] = 1; m[21] = 2; for (It it (m.begin()); it!=m.end(); ++it) cout << it->first << " " << it->second << endl; return 0; } However, I am looking for something that

How to slice one MultiIndex DataFrame with the MultiIndex of another

丶灬走出姿态 提交于 2019-11-27 04:30:03
问题 I have a pandas dataframe with 3 levels of a MultiIndex. I am trying to pull out rows of this dataframe according to a list of values that correspond to two of the levels. I have something like this: ix = pd.MultiIndex.from_product([[1, 2, 3], ['foo', 'bar'], ['baz', 'can']], names=['a', 'b', 'c']) data = np.arange(len(ix)) df = pd.DataFrame(data, index=ix, columns=['hi']) print(df) hi a b c 1 foo baz 0 can 1 bar baz 2 can 3 2 foo baz 4 can 5 bar baz 6 can 7 3 foo baz 8 can 9 bar baz 10 can

Reshape MultiIndex dataframe to tabular format

我的梦境 提交于 2019-11-27 03:48:25
问题 Given a sample MultiIndex: idx = pd.MultiIndex.from_product([[0, 1, 2], ['a', 'b', 'c', 'd']]) df = pd.DataFrame({'value' : np.arange(12)}, index=idx) df value 0 a 0 b 1 c 2 d 3 1 a 4 b 5 c 6 d 7 2 a 8 b 9 c 10 d 11 How can I efficiently convert this to a tabular format like so? a b c d 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 Furthermore, given the dataframe above, how can I bring it back to its original multi-indexed state? What I've tried: pd.DataFrame(df.values.reshape(-1, df.index.levels[1].size)