multi-index

pandas: pivot table inside multilevel dataframe

人盡茶涼 提交于 2020-01-05 05:26:09
问题 I'm trying to pivot table, in order to transform some rows values in columns, so from this dataframe df_behave list date_time field value 1 0 2015-05-22 05:37:59 StudentID 129 1 2015-05-22 05:37:59 SchoolId 3 2 2015-05-22 05:37:59 GroupeId 45 2 3 2015-05-26 05:56:59 StudentID 129 4 2015-05-26 05:56:59 SchoolId 65 5 2015-05-26 05:56:59 GroupeId 13 6 2015-05-26 05:56:59 Reference 87 3 ...................... ...... ...... in order to achieve : list date_time StudentID SchoolId GroupId Reference

Pandas Sort MultiIndex Pivot Table

三世轮回 提交于 2020-01-05 04:51:17
问题 Given the following pivot table: import pandas as pd import numpy as np df = pd.DataFrame( {'YYYYMM':[201603,201503,201403,201303,201603,201503,201403,201303], 'Count':[5,6,2,7,4,7,8,9], 'Group':['A','A','A','A','B','B','B','B']}) df['YYYYMM']=df['YYYYMM'].astype(str).str[:-2].astype(np.int64) t=df.pivot_table(df,index=['Group'],columns=['YYYYMM'],aggfunc=np.sum) t Count YYYYMM 2013 2014 2015 2016 Group A 7 2 6 5 B 9 8 7 4 I'd like to sort the rows (groups A and B) ascendingly by 2016 such

How to get a random (bootstrap) sample from pandas multiindex

∥☆過路亽.° 提交于 2020-01-03 12:24:29
问题 I'm trying to create a bootstrapped sample from a multiindex dataframe in Pandas. Below is some code to generate the kind of data I need. from itertools import product import pandas as pd import numpy as np df = pd.DataFrame({'group1': [1, 1, 1, 2, 2, 3], 'group2': [13, 18, 20, 77, 109, 123], 'value1': [1.1, 2, 3, 4, 5, 6], 'value2': [7.1, 8, 9, 10, 11, 12] }) df = df.set_index(['group1', 'group2']) print df The df dataframe looks like: value1 value2 group1 group2 1 13 1.1 7.1 18 2.0 8.0 20 3

Pandas Dataframe Multiindex Merge

老子叫甜甜 提交于 2020-01-03 07:19:09
问题 I wanted to ask a questions regarding merging multiindex dataframe in pandas, here is a hypothetical scenario: arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] tuples = list(zip(*arrays)) index1 = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) index2 = pd.MultiIndex.from_tuples(tuples, names=['third', 'fourth']) s1 = pd.DataFrame(np.random.randn(8), index=index1, columns=['s1']) s2 = pd.DataFrame(np

Get special group in pandas multiindex

随声附和 提交于 2020-01-03 03:20:11
问题 I have a DataFrame with MultiIndex like this: In [5]: df Out[5]: a b lvl0 lvl1 lvl2 A0 B0 C0 0 1 C1 2 3 C2 4 5 C3 6 7 B1 C0 8 9 C1 10 11 C2 12 13 C3 14 15 A1 B0 C0 16 47 C1 18 49 C2 20 41 C3 22 43 B1 C0 24 25 C1 26 27 C2 28 29 C3 30 31 A2 B0 C0 32 33 C1 34 35 C2 36 37 C3 38 39 B1 C0 40 41 C1 42 43 C2 44 45 C3 46 47 I want get the special lvl1 group in each lvl0 index. In this case, choice the group where column b has max value, result may like this: a b lvl0 lvl1 lvl2 A0 B1 C0 8 9 C1 10 11 C2

Boost Multi_Index Question

我是研究僧i 提交于 2020-01-02 07:28:50
问题 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

Using boolean indexing for row and column MultiIndex in Pandas

∥☆過路亽.° 提交于 2020-01-02 02:19:26
问题 Questions are at the end, in bold . But first, let's set up some data: import numpy as np import pandas as pd from itertools import product np.random.seed(1) team_names = ['Yankees', 'Mets', 'Dodgers'] jersey_numbers = [35, 71, 84] game_numbers = [1, 2] observer_names = ['Bill', 'John', 'Ralph'] observation_types = ['Speed', 'Strength'] row_indices = list(product(team_names, jersey_numbers, game_numbers, observer_names, observation_types)) observation_values = np.random.randn(len(row_indices)

How to apply condition on level of pandas.multiindex?

試著忘記壹切 提交于 2020-01-01 14:34:10
问题 My data looks like this ( ch = channel, det = detector): ch det time counts 1 1 0 123 2 0 121 3 0 125 2 1 0 212 2 0 210 3 0 210 1 1 1 124 2 1 125 3 1 123 2 1 1 210 2 1 209 3 1 213 Note, in reality, the time column is a float with 12 or so significant digits, still constant for all detectors of 1 measurement, but its value is not predictable, nor in a sequence. What I need to create is a data frame that looks like this: c time mean_counts_over_detectors 1 0 xxx 2 0 yyy 1 1 zzz 1 1 www I.e., I

Assigning values to Pandas Multiindex DataFrame by index level

柔情痞子 提交于 2020-01-01 10:30:15
问题 I have a Pandas multiindex dataframe and I need to assign values to one of the columns from a series. The series shares its index with the first level of the index of the dataframe. import pandas as pd import numpy as np idx0 = np.array(['bar', 'bar', 'bar', 'baz', 'foo', 'foo']) idx1 = np.array(['one', 'two', 'three', 'one', 'one', 'two']) df = pd.DataFrame(index = [idx0, idx1], columns = ['A', 'B']) s = pd.Series([True, False, True],index = np.unique(idx0)) print df print s out: A B bar one

Pandas: subtract one column from another in a pivot table

梦想的初衷 提交于 2019-12-31 04:53:09
问题 I would like to subtract one columns from another in a pivot table. 'diff' shoud be the difference between 2017 and 2016 raw_data = {'year': [2016,2016,2017,2017], 'area': ['A','B','A','B'], 'age': [10,12,50,52]} df1 = pd.DataFrame(raw_data, columns = ['year','area','age']) table=pd.pivot_table(df1,index=['area'],columns=['year'],values['age'],aggfunc='mean') table['diff']=table['2017']-table['2016'] 回答1: You need remove [] in pivot_table for dont create MultiIndex in columns: table=pd.pivot