multi-index

Slice pandas multiindex dataframe using list of index values [duplicate]

安稳与你 提交于 2019-12-31 03:43:09
问题 This question already has answers here : Select rows in pandas MultiIndex DataFrame (2 answers) Closed 12 months ago . I have a multi-index dataframe that looks like uid tid text abc x t1 bcd y t2 uid and tid are the indexes. I have a list of uid s, and want to get the rows corresponding to the uids in that list, but keeping the 2nd level index values (tid). I want to do it without running any explicit loop. Is that possible? 回答1: Data: L = ['abc', 'bcd'] print (df) text uid tid abc x t1 abc1

Replacing values in a pandas multi-index

喜夏-厌秋 提交于 2019-12-30 10:04:38
问题 I have a dataframe with a multi-index. I want to change the value of the 2nd index when certain conditions on the first index are met. I found a similar (but different) question here: Replace a value in MultiIndex (pandas) which doesn't answer my point because that was about changing a single row, and the solution passed the value of the first index (which didn't need changing), too. In my case I am dealing with multiple rows and I haven't been able to adapt that solution to my case. A

Resample pandas dataframe only knowing result measurement count

旧时模样 提交于 2019-12-25 05:33:15
问题 I have a dataframe which looks like this: Trial Measurement Data 0 0 12 1 4 2 12 1 0 12 1 12 2 0 12 1 12 2 NaN 3 12 I want to resample my data so that every trial has just two measurements So I want to turn it into something like this: Trial Measurement Data 0 0 8 1 8 1 0 12 1 12 2 0 12 1 12 This rather uncommon task stems from the fact that my data has an intentional jitter on the part of the stimulus presentation. I know pandas has a resample function, but I have no idea how to apply it to

Pandas multi index to datetime

旧时模样 提交于 2019-12-24 19:27:38
问题 How do I convert the following multiindex to datetime object and set that as the new index? df_gauge_mean.index Out[376]: MultiIndex(levels=[[2018, 2019], [1, 12], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]], labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

How do you calculate a (non-self) correlation matrix in pandas with multicolumns?

淺唱寂寞╮ 提交于 2019-12-24 07:34:47
问题 Suppose I have a pandas dataframe with multicolumns, like so: import pandas as pd iterables = [['a', 'b'], ['1', '2']] my_index = pd.MultiIndex.from_product(iterables, names=['first', 'second']) df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], columns=my_index) Then df produces first a b second 1 2 1 2 0 1 2 3 4 1 5 6 7 8 Now if I want the self-correlation of df['a'] with itself, that's straight-forward: df['a'].corr() gets me that. Note that such a correlation has shape (2, 2) . What I would

boost::multi_index composite keys efficiency

时间秒杀一切 提交于 2019-12-24 03:18:36
问题 Long time reader first time poster! I'm playing around with the boost::multi_index container stuff and have a rather in-depth question that hopefully a boost or C++ container expert might know (my knowledge in C++ containers is pretty basic). For reference, the boost documentation on composite keys can be found here: boost::multi_index composite keys. When using a composite key, the documentation states that "Composite keys are sorted by lexicographical order, i.e. sorting is performed by the

Pandas: custom class as column header with multi indexing

只谈情不闲聊 提交于 2019-12-24 00:59:35
问题 I'm trying to use objects as column headers in a multi indexed dataframe but I can't seem to get it to work. __eq__, __hash__ and __str__ only work for simple data frames. Here is a small example: class Signal: def __init__(self, name): self.name = name def __eq__(self, other): try: return self.name == other or self.name == other.name except AttributeError as err: return False def __str__(self): return str(self.name) def __hash__(self): return hash(self.name) if __name__ == '__main__': import

Sort dataframe multiindex level and by column

流过昼夜 提交于 2019-12-23 17:30:58
问题 Updated: pandas version 0.23.0 solves this problem with Sorting by a combination of columns and index levels I have struggled with this and I suspect there is a better way. How do I sort the following dataframe by index level name 'idx_0', level=0 and by column, 'value_1' descending such that the column 'MyName' reads vertical 'SCOTTBOSTON'. import pandas as pd import numpy as np df = pd.DataFrame({'idx_0':[2]*6+[1]*5, 'idx_1':[6,4,2,10,18,5,11,1,7,9,3], 'value_1':np.arange(11,0,-1), 'MyName'

Filtering rows by a particular index level in a MultiIndex dataframe

允我心安 提交于 2019-12-23 17:02:16
问题 Given a multIndex dataframe: mux = pd.MultiIndex.from_arrays([ list('aaaabbbbbccdddddd'), list('tuvwlmnopxyfghijk') ], names=['one', 'two']) df = pd.DataFrame({'col': np.arange(len(mux))}, mux) df col one two a t 0 u 1 v 2 w 3 b l 4 m 5 n 6 o 7 p 8 c x 9 y 10 d f 11 g 12 h 13 i 14 j 15 k 16 Is it possible to keep rows corresponding to upto the ith value of the 0th level of the dataframe? For i = 2, my expected output is: col one two a t 0 u 1 v 2 w 3 b l 4 m 5 n 6 o 7 p 8 Note that only rows

Filtering rows by a particular index level in a MultiIndex dataframe

血红的双手。 提交于 2019-12-23 16:57:02
问题 Given a multIndex dataframe: mux = pd.MultiIndex.from_arrays([ list('aaaabbbbbccdddddd'), list('tuvwlmnopxyfghijk') ], names=['one', 'two']) df = pd.DataFrame({'col': np.arange(len(mux))}, mux) df col one two a t 0 u 1 v 2 w 3 b l 4 m 5 n 6 o 7 p 8 c x 9 y 10 d f 11 g 12 h 13 i 14 j 15 k 16 Is it possible to keep rows corresponding to upto the ith value of the 0th level of the dataframe? For i = 2, my expected output is: col one two a t 0 u 1 v 2 w 3 b l 4 m 5 n 6 o 7 p 8 Note that only rows