multi-index

Pandas Replace All But Middle Values per Category of a Level with Blank

拜拜、爱过 提交于 2019-12-12 03:28:38
问题 Given the following pivot table: df=pd.DataFrame({'A':['a','a','a','a','a','b','b','b','b'], 'B':['x','y','z','x','y','z','x','y','z'], 'C':['a','b','a','b','a','b','a','b','a'], 'D':[7,5,3,4,1,6,5,3,1]}) table = pd.pivot_table(df, index=['A', 'B','C'],aggfunc='sum') table D A B C a x a 7 b 4 y a 1 b 5 z a 3 b x a 5 y b 3 z a 1 b 6 I know that I can access the values of each level like so: In [128]: table.index.get_level_values('B') Out[128]: Index(['x', 'x', 'y', 'y', 'z', 'x', 'y', 'z', 'z'

Pandas Multi-Index - Can't convert non-uniquely indexed DataFrame to Panel

醉酒当歌 提交于 2019-12-12 03:25:19
问题 Given a time series data, I'm trying to use panel OLS with fixed effects in Python. I found this way to do it: Fixed effect in Pandas or Statsmodels My input data looks like this (I will called it df ): Name Permits_13 Score_13 Permits_14 Score_14 Permits_15 Score_15 0 P.S. 015 ROBERTO CLEMENTE 12.0 284 22 279 32 283 1 P.S. 019 ASHER LEVY 18.0 296 51 301 55 308 2 P.S. 020 ANNA SILVER 9.0 294 9 290 10 293 3 P.S. 034 FRANKLIN D. ROOSEVELT 3.0 294 4 292 1 296 4 P.S. 064 ROBERT SIMON 3.0 287 15

Unstack Pandas DataFrame with MultiIndex

坚强是说给别人听的谎言 提交于 2019-12-12 01:54:47
问题 I have seen a lots of similar questions over here, but unfortunately I was not able to find the answers that could help me. What I'm trying to do is to reshape the DataFrame with MultiIndex via unstack() method. Here is it: val item indicator 0 Расположение: Минское шоссе /Минское шоссе Направление: Запад Площадь: 1200 м² Стоимость: 1 007 259 000 руб. 1 Расположение: Переделкино /Минское шоссе Направление: Запад Площадь: 850 м² Стоимость: 973 683 700 руб. 2 Расположение: Бородки /Минское

Pandas - flattening a multiindex column containing tuples, but ignore missing values

一个人想着一个人 提交于 2019-12-11 18:28:23
问题 I have a multiindex pandas dataframe like this: lst = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12), (13, 14), (21, 22)] df = pd.DataFrame(lst, pd.MultiIndex.from_product([['A', 'B'], ['1','2', '3', '4']])).loc[:('B', '2')] df["tuple"] = list(zip(df[0], df[1])) #df: 0 1 tuple A 1 1 2 (1, 2) 2 3 4 (3, 4) 3 5 6 (5, 6) 4 7 8 (7, 8) B 1 9 10 (9, 10) 2 11 12 (11, 12) I want to transform the column, containing the tuples, into a list of tuples. My approach is: #dataframe to append list of

How can I create a multiindex data frame with the following datasets?

孤街浪徒 提交于 2019-12-11 18:26:05
问题 I have to create a multi index data frame condisering data contained in two different data frames. For each index of the second data frame (Date), for each row of the first data frame, if the value in the column Date of the first data frame is equal to the index of the second data frame then create me a multi index dataframe with each date, the number of tweets published each day and the features of each row. This is the first data frame with Datas from Twitter: Date Full text Retweets Likes

How to conditionally reset a rolling max's initial value/row in pandas MultiIndex dataframe?

可紊 提交于 2019-12-11 15:44:07
问题 I have a multiindex dataframe. Index columns are Date and Symbol . I want to reset the row where the dataframe starts to evaluate rolling_max of number for each Symbol . I want to do this based on a column containing True or False . If condition is True on a Date then rolling_max should be reset and calculate max from this Date . If condition is False then rolling_max should work 'normally' - taking the max of today's and yesterday's number for the given Symbol . The condition column has

Boost Multi-Index : Composite key of vector and int for hashed indices

拟墨画扇 提交于 2019-12-11 14:18:23
问题 as I've just learned in in my other question, I could use a composite_key for a struct, which has a std::vector and an integer. Now my question is: Can I use this somehow to work with hashed_indecies? Here an example similar to THIS: struct unique_property { //the pair of int and std::vector<int> shall be unique int my_int; std::vector<int> my_vec; }; typedef multi_index_container< unique_property, indexed_by< hashed_unique< // indexed by my_int and every entry of my_vec composite_key< street

Sub-select a multi-index pandas dataframe to create multiple subsets (using a dictionary)

狂风中的少年 提交于 2019-12-11 12:46:53
问题 I have a dataset similar to the following: df_lenght = 240 df = pd.DataFrame(np.random.randn(df_lenght,2), columns=['a','b'] ) df['datetime'] = pd.date_range('23/06/2017', periods=df_lenght, freq='H') unique_jobs = ['job1','job2','job3',] job_id = [unique_jobs for i in range (1, int((df_lenght/len(unique_jobs))+1) ,1) ] df['job_id'] = sorted( [val for sublist in job_id for val in sublist] ) df.set_index(['job_id','datetime'], append=True, inplace=True) print(df[:5]) returns: a b job_id

How do I update overlapping indexes in a boost::multi_index_container?

断了今生、忘了曾经 提交于 2019-12-11 11:40:33
问题 I'm using boost 1.48.0, and I don't have the option to upgrade. I've built a multi_index_container with indexes that have overlapping criteria. How are indexes sharing the same indexing criteria effected, as in the following example? The last line of the example code alludes to what I am asking for. struct street_address_key : composite_key< t_postal_address , const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::street_name>> , const_mem_fun<const_mem_fun<t_postal_address,

boost::multi_index_container: Find index iterator from other index iterator

淺唱寂寞╮ 提交于 2019-12-11 11:16:13
问题 I have a multi_index_container indexed by 2 indexes. I'm able to find the value by one of them, but is it possible to find the iterator from the other corresponding index? Example: struct ById{}; struct ByName{}; typedef multi_index_container< MyStruct, indexed_by< ordered_unique<tag<ById>, member< MyStruct, int, &MyStruct::id> >, ordered_non_unique<tag<BySalary>, member< MyStruct, int, &MyStruct::salary> > > > MyStructsContainer; typedef MyStructsContainer::index<ById>::type