multi-index

Why do I see all original index elements in a sliced dataframe? [duplicate]

ε祈祈猫儿з 提交于 2019-12-10 21:28:31
问题 This question already has answers here : How do you update the levels of a pandas MultiIndex after slicing its DataFrame? (3 answers) Closed 2 years ago . I have a multiindex dataframe like this: import pandas as pd import numpy as np df = pd.DataFrame({'ind1': list('aaaaaaaaabbbbbbbbb'), 'ind2': list('cccdddeeecccdddeee'), 'ind3': list(range(3))*6, 'val1': list(range(100, 118)), 'val2': list(range(70, 88))}) df_mult = df.set_index(['ind1', 'ind2', 'ind3']) val1 val2 ind1 ind2 ind3 a c 0 100

Searching in a set of shared_ptr<QString>

不问归期 提交于 2019-12-10 20:14:20
问题 I have an object: class Object { public: boost::shared_ptr<QString> const& name() const {reutrn _name;} private: boost::shared_ptr<QString> _name; }; And a multi_index set typedef boost::multi_index_container< Object, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::const_mem_fun< Object, boost::shared_ptr<QString> const&, & Object::name>, StringPointerLess> > > ObjectSet; Now If I want to find something in the set and I have QString I need to make a

search for multiple indecies with Boost Multi-Index

前提是你 提交于 2019-12-10 19:29:49
问题 how do I limit the search in a boost::multi_index by the result of a previous search? As an example: suppose I have a rectangle class with an internal value like this: class MyRect { public: int width; int height; double value; } and I need a data structure of such object to answer queries like "given an input_rectangle - which object MyRect is contained in that rectangle and has the highest value?" I could use a 'multi_index' like this: struct given_value{}; struct given_width{}; struct

Create MultiIndexed dataframe through constructor

血红的双手。 提交于 2019-12-10 19:29:08
问题 Given two arrays: x [('010_628', '2543677'), ('010_228', '2543677'), ('015_634', '2543677')] y array([['me', 10228955], ['me', 10228955], ['me', 10228955]], dtype=object) Currently, this code gets me a dataframe with a flat index of tuples: df = pd.DataFrame(x, index=y, columns=['pm_code', 'sec_pm']) df pm_code sec_pm (me, 10228955) 010_628 2543677 (me, 10228955) 010_228 2543677 (me, 10228955) 015_634 2543677 How can I instead create a MultiIndex dataframe that looks like this? pm_code sec_pm

DataFrame.index.levels shows “extra” values after paring down dataframe

◇◆丶佛笑我妖孽 提交于 2019-12-10 18:36:47
问题 Let's say I have a large dataframe large that has a MultiIndex on the rows. I pare down this dataframe by selecting only some of the rows and assign the result to small . In particular, small has fewer distinct values in the 0th level of its MultiIndex on the rows than large . I then want a list of the distinct values in the 0th level of the MultiIndex on the rows of small so I call small.index.levels[0] . The result is strange: it returns the same thing as large.index.levels[0] despite the

Make column from pandas dataframe index

心不动则不痛 提交于 2019-12-10 17:26:35
问题 I have a dataframe where I would like to turn the data in the (first level of the) index into a column. Practically my df looks like this: col1 CoI AK 0 1 1 31 2 NaN BB 0 5 1 31 2 NaN And I would like to turn it into this: col1 CoI 0 1 AK 1 31 AK 2 NaN AK 0 5 BB 1 31 BB 2 NaN BB How can I best do this? I think this is a rather basic functionality, but as with many other "basic" pandas things I cannot find info on this anywhere. Many Thanks, 回答1: df.reset_index(level=0, inplace=True) should do

How to insert a row in a Pandas multiindex dataframe?

拥有回忆 提交于 2019-12-10 16:03:00
问题 I have a Pandas dataframe with a multiindex (Reg, Type, Part, IsExpired)- Reg Type Part IsExpired Quantity APAC Disk A False 10 True 12 EMEA Disk A False 22 EMEA Disk B False 13 True 17 I want to make sure that every (Reg, Type, Part) tuple has True and False for IsExpired. E.g. I'd like to insert a row for (EMEA, Disk, A, True)- Reg Type Part IsExpired Quantity APAC Disk A False 10 True 12 EMEA Disk A False 22 True 0 <-- inserted row EMEA Disk B False 13 True 17 回答1: You could unstack and

Reindexing a specific level of a MultiIndex dataframe

岁酱吖の 提交于 2019-12-10 14:57:24
问题 I have a DataFrame with two indices and would like to reindex it by one of the indices. from pandas_datareader import data import matplotlib.pyplot as plt import pandas as pd # Instruments to download tickers = ['AAPL'] # Online source one should use data_source = 'yahoo' # Data range start_date = '2000-01-01' end_date = '2018-01-09' # Load the desired data panel_data = data.DataReader(tickers, data_source, start_date, end_date).to_frame() panel_data.head() The reindexing goes as follows: #

Pandas MultiIndex DataFrame.rolling offset

核能气质少年 提交于 2019-12-10 14:54:33
问题 Why can't I use an offset when rolling a multi-index DataFrame? For example, with: rng = pd.date_range('2017-01-03', periods=20, freq='8D') i = pd.MultiIndex.from_product([['A','B','C'], rng], names=['Name','Date']) df = pd.DataFrame(np.random.randn(60), i, columns=['Vals']) If I try grouping and rolling with an offset I get " ValueError: window must be an integer ": df['Avg'] = df.groupby(['Name'])['Vals'].rolling('30D').mean() # << Why doesn't this work? Not that these following variants

Sort pandas DataFrame with MultiIndex according to column value

喜你入骨 提交于 2019-12-10 13:11:44
问题 I have a DataFrame with MultiIndex looking like this after printing in the console: value indA indB scenarioId group 2015-04-13 1 A -54.0 1.0 1.0 B -160.0 1.0 1.0 C -15.0 0.0 1.0 2 A -83.0 1.0 1.0 3 A -80.0 2.0 2.0 4 A -270.0 2.0 2.0 2015-04-14 1 A -56.0 1.0 1.0 B -1.0 1.0 1.0 C -60.0 0.0 1.0 2 A -32.0 1.0 1.0 3 A -91.0 2.0 2.0 4 A -17.0 2.0 2.0 I got it after I used the groupby and sum functions on my initial dataset. I would like to keep the same format, but order it according to the value