multi-index

Creating new columns in sublevel of multindex pandas columns

柔情痞子 提交于 2019-12-11 09:07:36
问题 I have a multindex column. Higher level is some humans, sublevel is some measures. I would like to create some new columns which are derivatives of measures (eg. rolling mean). I was hoping I could use some index slicing to achieve this, but alas now. I've found some similar-ish questions here in the past, but they were old questions, and I suspect there are more modern, pythonic solutions. Below is toy example where I demonstrate what I'm trying to do for one column (which works) but shows

How to select a subset from a Multi-Index Dataframe based on conditions from another DataFrame

旧城冷巷雨未停 提交于 2019-12-11 07:28:06
问题 I have a dataframe like below: dates 0 numbers letters 0 a 2013-01-01 0.261092 2013-01-02 -1.267770 2013-01-03 0.008230 b 2013-01-01 -1.515866 2013-01-02 0.351942 2013-01-03 -0.245463 c 2013-01-01 -0.253103 2013-01-02 -0.385411 2013-01-03 -1.740821 1 a 2013-01-01 -0.108325 2013-01-02 -0.212350 2013-01-03 0.021097 b 2013-01-01 -1.922214 2013-01-02 -1.769003 2013-01-03 -0.594216 c 2013-01-01 -0.419775 2013-01-02 1.511700 2013-01-03 0.994332 2 a 2013-01-01 -0.020299 2013-01-02 -0.749474 2013-01

Pandas multiple index dataframe: creating new index or appending to existing index

浪尽此生 提交于 2019-12-11 06:30:01
问题 I have a Pandas dataframe, multi_df , which has a multi-index made of the code , colour , texture and shape values as below: import pandas as pd import numpy as np df = pd.DataFrame({'id' : range(1,9), 'code' : ['one', 'one', 'two', 'three', 'two', 'three', 'one', 'two'], 'colour': ['black', 'white','white','white', 'black', 'black', 'white', 'white'], 'texture': ['soft', 'soft', 'hard','soft','hard', 'hard','hard','hard'], 'shape': ['round', 'triangular', 'triangular','triangular','square',

Pandas Multiindex from array => TypeError: unhashable type: 'dict'

流过昼夜 提交于 2019-12-11 03:46:43
问题 I'm trying to create the dataframe from the array with following structure: df = [[{'date_time': Timestamp('2015-05-22 05:37:59'), 'name': 'Tom', 'value': '129'}, {'date_time': Timestamp('2015-05-22 05:37:59'), 'name': 'Kate', 'value': '0'}, {'date_time': Timestamp('2015-05-22 05:37:59'), 'name': 'GroupeId', 'value': '0'}, {...}, {...}, {...}],[another list of dictionaries like the first one],[and another one]] using this code: def create_from_arr(): baby_array=pd.MultiIndex.from_tuples(df,

Replace a value in MultiIndex (pandas)

五迷三道 提交于 2019-12-11 03:27:17
问题 In the following DataFrame: How can I replace ["x2", "Total"] with ["x2", "x2"] leaving x1 as is? l1 900 902 912 913 916 l2 ИП ПС ИП ПС ИП ПС ИП ПС ИП ПС i1 i2 x1 Total 10 6 3 3 10 16 2 9 3 8 x2 Total 1 0 0 0 0 0 0 0 0 0 .rename will replace all "Total" values, not just the one I need. 回答1: Assuming your dataframe is called df the following code will perform your desired substitution by replacing the existing index with a modified index. index = df.index names = index.names index = df.index

boost::multi_index_container with random_access and ordered_unique

帅比萌擦擦* 提交于 2019-12-11 03:10:11
问题 I have a problem getting boost::multi_index_container work with random-access and with orderd_unique at the same time. (I'm sorry for the lengthly question, but I think I should use an example..) Here an example: Suppose I want to produce N objects in a factory and for each object I have a demand to fulfill (this demand is known at creation of the multi-index). Well, within my algorithm I get intermediate results, which I store in the following class: class intermediate_result { private: std:

Pandas multi-index subtract from value based on value in other column

余生颓废 提交于 2019-12-11 02:37:55
问题 Given the following data frame: df = pd.DataFrame({ ('A', 'a'): [23, 'n/a',54,7,32,76], ('B', 'b'): [23, 'n/a',54,7,32,76], ('possible','possible'):[100,100,100,100,100,100] }) df A B possible a b 0 23 23 100 1 n/a n/a 100 2 54 54 100 3 7 n/a 100 4 32 32 100 5 76 76 100 I'd like to adjust 'possible', per row, for every instance of 'n/a' such that each instance will subtract 4 from 'possible'. The desired result is as follows: A B possible a b possible 0 23 23 100 1 n/a n/a 92 2 54 54 100 3 7

Python Pandas sorting multiindex by column, but retain tree structure

北战南征 提交于 2019-12-11 01:49:38
问题 Using pandas 0.20.3 I am trying to sort the n multilevels of a dataframe by a column ('D') with values (descendlingly) such that the hierarchy of the groups is maintained. Example input: D A B C Gran1 Par1 Child1 3 Child2 7 Child3 2 Par2 Child1 9 Child2 2 Par3 Child1 6 Gran2 Par1 Child1 3 Par2 Child1 8 Child2 2 Child3 3 Par3 Child1 6 Child2 8 Desired result: D A B C Gran2 Par3 Child2 8 Child1 6 Par2 Child1 8 Child3 3 Child2 2 Par1 Child1 3 Gran1 Par1 Child2 7 Child1 3 Child3 2 Par2 Child1 9

Slicing and assigning values multi-indexed pandas dataframe of unique sequential indices

馋奶兔 提交于 2019-12-11 01:16:43
问题 I want to select and change the value of a dataframe cell. There are 2 indices used for this dataframe: 'datetime' and 'idx'. Both contain labels which are unique and sequential. 'datetime' index has datetime label of datetime type, and 'idx' has integer valued labels. import numpy as np import pandas as pd dt = pd.date_range("2010-10-01 00:00:00", periods=5, freq='H') d = {'datetime': dt, 'a': np.arange(len(dt))-1,'b':np.arange(len(dt))+1} df = pd.DataFrame(data=d) df.set_index(keys=

Sum duplicated rows on a multi-index pandas dataframe

帅比萌擦擦* 提交于 2019-12-11 00:22:32
问题 Hello I'm having troubles dealing with Pandas. I'm trying to sum duplicated rows on a multiindex Dataframe. I tryed with df.groupby(level=[0,1]).sum() , also with df.stack().reset_index().groupby(['year', 'product']).sum() and some others, but I cannot get it to work. I'd also like to add every unique product for each given year and give them a 0 value if they weren't listed. Example: dataframe with multi-index and 3 different products (A,B,C): volume1 volume2 year product 2010 A 10 12 A 7 3