multi-index

Multi-Index Sorting in Pandas

牧云@^-^@ 提交于 2019-11-30 10:41:28
问题 I have a multi-index DataFrame created via a groupby operation. I'm trying to do a compound sort using several levels of the index, but I can't seem to find a sort function that does what I need. Initial dataset looks something like this (daily sales counts of various products): Date Manufacturer Product Name Product Launch Date Sales 0 2013-01-01 Apple iPod 2001-10-23 12 1 2013-01-01 Apple iPad 2010-04-03 13 2 2013-01-01 Samsung Galaxy 2009-04-27 14 3 2013-01-01 Samsung Galaxy Tab 2010-09-02

Python Pandas slice multiindex by second level index (or any other level)

放肆的年华 提交于 2019-11-30 07:16:55
There are many postings on slicing the level[0] of a multiindex by a range of level 1 . However, I cannot find a solution for my problem; that is, I need a range of the level 1 index for level[0] index values dataframe: First is A to Z, Rank is 1 to 400; I need the first 2 and last 2 for each level[0] (First), but not in the same step. Title Score First Rank A 1 foo 100 2 bar 90 3 lime 80 4 lame 70 B 1 foo 400 2 lime 300 3 lame 200 4 dime 100 I am trying to get the last 2 rows for each level 1 index with the below code, but it slices properly only for the first level[0] value. [IN] df.ix[x

Using .loc with a MultiIndex in pandas?

半城伤御伤魂 提交于 2019-11-30 06:15:30
Does anyone know if it is possible to use the DataFrame.loc method to select from a MultiIndex? I have the following DataFrame and would like to be able to access the values located in the 'Dwell' columns, at the indices of ('at', 1) , ('at', 3) , ('at', 5) , and so on (non-sequential). I'd love to be able to do something like data.loc[['at',[1,3,5]], 'Dwell'] , similar to the data.loc[[1,3,5], 'Dwell'] syntax for a regular index (which returns a 3-member series of Dwell values). My purpose is to select an arbitrary subset of the data, perform some analysis only on that subset, and then update

Transform Pandas DataFrame with n-level hierarchical index into n-D Numpy array

删除回忆录丶 提交于 2019-11-30 04:56:50
问题 Question Is there a good way to transform a DataFrame with an n -level index into an n -D Numpy array (a.k.a n -tensor)? Example Suppose I set up a DataFrame like from pandas import DataFrame, MultiIndex index = range(2), range(3) value = range(2 * 3) frame = DataFrame(value, columns=['value'], index=MultiIndex.from_product(index)).drop((1, 0)) print frame which outputs value 0 0 0 1 1 2 3 1 1 5 2 6 The index is a 2-level hierarchical index. I can extract a 2-D Numpy array from the data using

Read multi-index on the columns from csv file

六眼飞鱼酱① 提交于 2019-11-30 03:18:44
I have a .csv file that looks like this: Male, Male, Male, Female, Female R, R, L, R, R .86, .67, .88, .78, .81 I want to read that into a df, so that I have: Male Female R L R 0 .86 .67 .88 .78 .81 I did: df = pd.read_csv('file.csv', header=[0,1]) But headers does not cut it. Which results in Empty DataFrame Columns: [(Male, R), (Male, R), (Male, L), (Female, R), (Female, R)] Index: [] Yet, the docs on headers says: (...)Can be a list of integers that specify row locations for a multi-index on the columns E.g. [0,1,3] What am I doing wrong? How can I possibly make it work? I think the problem

How to do Multi-Column from_tuples?

久未见 提交于 2019-11-30 03:13:57
问题 I get how to use pd.MultiIndex.from_tuples() in order to change something like Value (A,a) 1 (B,a) 2 (B,b) 3 into Value Caps Lower A a 1 B a 2 B b 3 But how do I change column tuples in the form (A, a) (A, b) (B,a) (B,b) index 1 1 2 2 3 2 2 3 3 2 3 3 4 4 1 into the form Caps A B Lower a b a b index 1 1 2 2 3 2 2 3 3 2 3 3 4 4 1 Many thanks. Edit: The reason I have a tuple column header is that when I joined a DataFrame with a single level column onto a DataFrame with a Multi-Level column it

Multi-Index Sorting in Pandas

萝らか妹 提交于 2019-11-29 21:55:57
I have a multi-index DataFrame created via a groupby operation. I'm trying to do a compound sort using several levels of the index, but I can't seem to find a sort function that does what I need. Initial dataset looks something like this (daily sales counts of various products): Date Manufacturer Product Name Product Launch Date Sales 0 2013-01-01 Apple iPod 2001-10-23 12 1 2013-01-01 Apple iPad 2010-04-03 13 2 2013-01-01 Samsung Galaxy 2009-04-27 14 3 2013-01-01 Samsung Galaxy Tab 2010-09-02 15 4 2013-01-02 Apple iPod 2001-10-23 22 5 2013-01-02 Apple iPad 2010-04-03 17 6 2013-01-02 Samsung

Showing all index values when using multiIndexing in Pandas

我怕爱的太早我们不能终老 提交于 2019-11-29 15:39:21
I would like that when viewing my DataFrame I will see all values of the multiIndex, including when subsequent rows have the same index for one of the levels. Here is an example: arrays = [['20', '50', '20', '20'],['N/A', 'N/A', '10', '30']] tuples = list(zip(*arrays)) index = pd.MultiIndex.from_tuples(tuples, names=['Jim', 'Betty']) pd.DataFrame([np.random.rand(1)]*4,index=index) The output is: 0 Jim Betty 20 N/A 0.954973 50 N/A 0.954973 20 10 0.954973 30 0.954973 I would like to have a 20 also in the south-west corner. That is, I would like my DataFrame to be: 0 Jim Betty 20 N/A 0.954973 50

using boost multi_index_container to preserve insertion order

扶醉桌前 提交于 2019-11-29 11:11:34
I initially started out using a std::multimap to store many values with the same key, but then I discovered that it doesn't preserve the insertion order among values with the same key. This answer claims it can be done with boost::multi_index::multi_index_container , but gives no example. Looking through the docs, there are no examples of that usage, and I can't make heads or tails of how you're supposed to use this thing. I have come to expect poor documentation from the lesser-used boost libraries, but this takes the cake. Can anyone point me to a tutorial or example that shows it used the

Python Pandas slice multiindex by second level index (or any other level)

随声附和 提交于 2019-11-29 09:24:33
问题 There are many postings on slicing the level[0] of a multiindex by a range of level1. However, I cannot find a solution for my problem; that is, I need a range of the level1 index for level[0] index values dataframe: First is A to Z, Rank is 1 to 400; I need the first 2 and last 2 for each level[0] (First), but not in the same step. Title Score First Rank A 1 foo 100 2 bar 90 3 lime 80 4 lame 70 B 1 foo 400 2 lime 300 3 lame 200 4 dime 100 I am trying to get the last 2 rows for each level1