multi-index

Reindex sublevel of pandas dataframe multiindex

冷暖自知 提交于 2019-12-07 02:01:13
问题 I have a time series dataframe and I would like to reindex it by Trials and Measurements. Simplified, I have this: value Trial 1 0 13 1 3 2 4 2 3 NaN 4 12 3 5 34 Which I want to turn into this: value Trial 1 0 13 1 3 2 4 2 0 NaN 1 12 3 0 34 How can I best do this? 回答1: Just yesterday, the illustrious Andy Hayden added this feature to version 0.13 of pandas, which will be released any day now. See here for usage example he added to the docs. If you are comfortable installing the development

Pandas Add Header Row for MultiIndex

陌路散爱 提交于 2019-12-06 16:31:19
Given the following data frame: d2=pd.DataFrame({'Item':['y','y','z','x'], 'other':['aa','bb','cc','dd']}) d2 Item other 0 y aa 1 y bb 2 z cc 3 x dd I'd like to add a row to the top and then use that as level 1 of a multiIndexed header. I can't always predict how many columns the data frame will have, so the new row should allow for that (i.e. random characters or numbers are okay). I'm looking for something like this: Item other A B 0 y aa 1 y bb 2 z cc 3 x dd But again, the number of columns will vary and cannot be predicted. Thanks in advance! I think you can first find number of columns by

Split rows according to text in two columns (Python, Pandas)

送分小仙女□ 提交于 2019-12-06 11:47:41
This is my dataframe (with many more letters and a length of ~35.5k) and stuff where the – are other relevant strings). All the variables are strings and ['C1','C2'] is the MultiIndex. tmp C1 C2 C3 C4 C5 Start End C8 A 1 - - - 12 14 - A 2 - - - 1,4,7 3,6,10 - A 3 - - - 16,19 17,21 - A 4 - - - 22 24 - I need it to become this (split every row that contains commas maintaining everything else): C1 C2 C3 C4 C5 Start End C8 Appearance A 1 - - - 12 14 - 1 A 2 - - - 1 3 - 1 A 2 - - - 4 6 - 2 A 2 - - - 7 10 - 3 A 3 - - - 16 17 - 1 A 3 - - - 19 21 - 2 A 4 - - - 22 24 - 1 I tried this script pandas: How

Multi-Indexed fillna in Pandas

拜拜、爱过 提交于 2019-12-06 09:24:41
问题 I have a multi-indexed dataframe and I'm looking to backfill missing values within a group. The dataframe I have currently looks like this: df = pd.DataFrame({ 'group': ['group_a'] * 7 + ['group_b'] * 3 + ['group_c'] * 2, 'Date': ["2013-06-11", "2013-07-02", "2013-07-09", "2013-07-30", "2013-08-06", "2013-09-03", "2013-10-01", "2013-07-09", "2013-08-06", "2013-09-03", "2013-07-09", "2013-09-03"], 'Value': [np.nan, np.nan, np.nan, 9, 4, 40, 18, np.nan, np.nan, 5, np.nan, 2]}) df.Date = df[

Pandas Multiindex dataframe remove rows

萝らか妹 提交于 2019-12-06 07:28:44
I have Multiiindex DF as follows: tuples = list(zip(*[['a', 'a', 'b', 'b'], ['c', 'd', 'c', 'd']])) index = pd.MultiIndex.from_tuples(tuples, names=['i1', 'i2']) df = pd.DataFrame([5, 6, 7, 8], index=index[:4], columns=['col']) col i1 i2 a c 5 d 6 b c 7 d 8 Would like to keep rows whose index (level 0) is in idx_to_keep = ['a'] Should be a straightforward task, but I can't think of any other way than idx_to_drop = np.setdiff1d(pd.unique(df.index.levels[0]), idx_to_keep) df.drop(idx_to_drop, inplace = True) col i1 i2 a c 5 d 6 Can I do better? One way is to use the index method get_level_values

Boost Multi_Index Question

坚强是说给别人听的谎言 提交于 2019-12-06 03:56:58
Sorry I can't be more specific in the title. Let's say I have a class Foo class Foo { public: Foo() { m_bitset.reset(); } void set_i(int i) { m_bitset.set(1); m_i = i; } void set_j(int j) { m_bitset.set(2); m_j = j; } bool i_set() { return m_bitset(1); } bool j_set() { return m_bitset(2); } void clear_i() { m_bitset.reset(1); } void clear_j() { m_bitset.reset(2); } int get_i() { assert(i_set()); return m_i; } int get_j() { assert(j_set()); return m_j; } private: int m_i, m_j; bitset<2> m_bitset; }; And now I want to put Foo's into a multi_index. typedef multi_index_container < Foo, indexed_by<

How to iterate over MultiIndex levels in Pandas?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 03:27:48
问题 I often have MultiIndex indices and I'd like to iterate over groups where higher level indices are equal. It basically looks like from random import choice import pandas as pd N = 100 df = pd.DataFrame([choice([1, 2, 3]) for _ in range(N)], columns=["A"], index=pd.MultiIndex.from_tuples([(choice("ab"), choice("cd"), choice("de")) for _ in range(N)])) for idx in zip(df.index.get_level_values(0), df.index.get_level_values(1)): df_select = df.ix[idx] Is there a way to do the for loop iteration

Slice MultiIndex pandas DataFrame by position

寵の児 提交于 2019-12-05 18:13:48
I am currently trying to to slice a MuliIndex DataFrame that has three levels by position. I am using pandas 19.1 Level0 Level1 Level2 Value 03-00368 A Item111 6.9 03-00368 A Item333 19.2 03-00368 B Item111 9.7 03-00368 B Item222 17.4 04-00176 C Item110 17.4 04-00176 C Item111 9.7 04-00246 D Item46 12.5 04-00246 D Item66 5.6 04-00246 D Item99 11.2 04-00247 E Item23 12.5 04-00247 E Item24 5.6 04-00247 E Item111 11.2 04-00247 F Item23 7.9 04-00247 F Item24 9.7 04-00247 F Item111 12.5 04-00247 G Item46 11.2 04-00247 G Item66 9.7 04-00247 G Item999 9.7 04-00247 H Item23 11.2 04-00247 H Item94 7.9

Merge two MultiIndex levels into one in Pandas

試著忘記壹切 提交于 2019-12-05 17:39:06
问题 I have a Pandas data frame which is MultiIndexed. The second level contains a year ([2014,2015]) and the third contains the month number ([1, 2, .., 12]). I would like to merge these two into a single level like - [1/2014, 2/2014 ..., 6/2015]. How could this be done? I'm new to Pandas. Searched a lot but could not find any similar question/solution. Edit: I found a way to avoid having to do this altogether with the answer to this question. I should have been creating my data frame that way.

High-dimensional data structure in Python

…衆ロ難τιáo~ 提交于 2019-12-05 13:38:22
What is best way to store and analyze high-dimensional date in python? I like Pandas DataFrame and Panel where I can easily manipulate the axis. Now I have a hyper-cube (dim >=4) of data. I have been thinking of stuffs like dict of Panels, tuple as panel entries. I wonder if there is a high-dim panel thing in Python. update 20/05/16: Thanks very much for all the answers. I have tried MultiIndex and xArray, however I am not able to comment on any of them. In my problem I will try to use ndarray instead as I found the label is not essential and I can save it separately. update 16/09/16: I came