multi-index

Pandas Multiindex count on levels

谁都会走 提交于 2021-02-05 10:59:47
问题 The data: index = [('A', 'aa', 'aaa'), ('A', 'aa', 'aab'), ('B', 'bb', 'bbb'), ('B', 'bb', 'bbc'), ('C', 'cc', 'ccc') ] values = [0.07, 0.04, 0.04, 0.06, 0.07] s = pd.Series(data=values, index=pd.MultiIndex.from_tuples(index)) s A aa aaa 0.07 aab 0.04 B bb bbb 0.04 bbc 0.06 C cc ccc 0.07 To get a mean of first two levels is easy: s.mean(level=[0,1]) Result: A aa 0.055 B bb 0.050 C cc 0.070 But to get a count on first two levels does not work the same: #s.count(level=[0,1]) # does not work I

Pandas Rename a Single Row of MultiIndex by Tuple

孤街浪徒 提交于 2021-02-05 06:57:25
问题 I'm trying to rename a single row of a pandas dataframe by it's tuple. For example: import pandas as pd df = pd.DataFrame(data={'i1':[0,0,0,0,1,1,1,1], 'i2':[0,1,2,3,0,1,2,3], 'x':[1.,2.,3.,4.,5.,6.,7.,8.], 'y':[9,10,11,12,13,14,15,16]}) df.set_index(['i1','i2'], inplace=True) Creates df: x y i1 i2 0 0 1.0 9 1 2.0 10 2 3.0 11 3 4.0 12 1 0 5.0 13 1 6.0 14 2 7.0 15 3 8.0 16 I'd like to be able to use something like: df.rename(index={(0,1):(0,9)},inplace=True) to get: x y i1 i2 0 0 1.0 9 9 2.0

Reorder Multi-indexed dataframe columns based on reference

血红的双手。 提交于 2021-02-04 19:55:07
问题 I have a multi-indexed dataframe with names attached to the column levels. The data table looks something like this: (df1) TIME TMC 111N1 111P2 111N3 111P4 DATE EPOCH 0 143 113 103 NaN 1 183 NaN NaN NaN 2 NaN NaN NaN NaN 3 143 NaN NaN NaN I'd like to shuffle the columns around so that they match the order specified by the rows index of a reference dataframe (df2): A1 A2 A3 A4 A5 Name 111N3 PA PL er 0.75543 35 111P4 PA PL er 0.09413 35 111N1 PA PL er 4.21557 35 111P2 PA PL er 1.31989 35 i.e.

Creating a multi-header table from JSON

不羁的心 提交于 2021-01-29 03:05:49
问题 I'm new to pandas. I'm trying to create the following table: | C | Perl | | line | func | line | func | version | covered | total | covered | total | There is the following structure of JSON that I have created: { "version1":{ "perl":{ "line_covered":207, "line_total":312, "func_covered":15, "func_total":18 }, "C":{ "line_covered":321, "line_total":512, "func_covered":10, "func_total":10 } } } I want to iterate over this JSON and create the table. My first problem is that I can't seem to

How to get the distinct count of first key in boost::multi_index_container with composite key

萝らか妹 提交于 2021-01-28 21:27:11
问题 boost::multi_index_container enables the construction of containers maintaining one or more indices with different sorting and access semantics like relational database. And I use boost::multi_index_container with composite key to handle something like this: struct Person { Person(int id, string name): m_id(id), m_name(name) { } int m_id; string m_name; }; typedef multi_index_container< Person, indexed_by< ordered_unique< member<Person, int, &Person::m_id> >, ordered_unique< composite_key<

How can I summarize several pandas dataframe columns into a parent column name?

二次信任 提交于 2021-01-28 19:41:43
问题 I've a dataframe which looks like this some feature another feature label sample 0 ... ... ... and I'd like to get a dataframe with multiindexed columns like this features label sample some another 0 ... ... ... From the API it's not clear to me how to use from_arrays() , from_product() , from_tuples() or from_frame() correctly. The solution shall not depend on string parsing of the feature columns ( some feature , another feature ). The last column for the label is the last column and it's

Want MultiIndex for rows and columns with read_csv

南笙酒味 提交于 2021-01-28 12:57:24
问题 My .csv file looks like: Area When Year Month Tickets City Day 2015 1 14 City Night 2015 1 5 Rural Day 2015 1 18 Rural Night 2015 1 21 Suburbs Day 2015 1 15 Suburbs Night 2015 1 21 City Day 2015 2 13 containing 75 rows. I want both a row multiindex and column multiindex that looks like: Area City Rural Suburbs When Day Night Day Night Day Night Year Month 2015 1 5.0 3.0 22.0 11.0 13.0 2.0 2 22.0 8.0 4.0 16.0 6.0 18.0 3 26.0 25.0 22.0 23.0 22.0 2.0 2016 1 20.0 25.0 39.0 14.0 3.0 10.0 2 4.0 14

Want MultiIndex for rows and columns with read_csv

廉价感情. 提交于 2021-01-28 12:57:01
问题 My .csv file looks like: Area When Year Month Tickets City Day 2015 1 14 City Night 2015 1 5 Rural Day 2015 1 18 Rural Night 2015 1 21 Suburbs Day 2015 1 15 Suburbs Night 2015 1 21 City Day 2015 2 13 containing 75 rows. I want both a row multiindex and column multiindex that looks like: Area City Rural Suburbs When Day Night Day Night Day Night Year Month 2015 1 5.0 3.0 22.0 11.0 13.0 2.0 2 22.0 8.0 4.0 16.0 6.0 18.0 3 26.0 25.0 22.0 23.0 22.0 2.0 2016 1 20.0 25.0 39.0 14.0 3.0 10.0 2 4.0 14

pandas reindex multiindex and shift values by second index

你离开我真会死。 提交于 2021-01-28 06:08:49
问题 I have a pandas DataFrame looking like this : x1 x2 x3 x4 Date Time 2017-01-03 09:00:00 0.000097 0.000259 0.000629 0.000142 09:20:00 0.000046 0.000044 0.000247 0.000134 09:40:00 0.000021 0.000032 0.000171 0.000105 10:00:00 0.000033 0.000040 0.000136 0.000178 10:20:00 0.000079 0.000157 0.000094 0.000083 ..... 17:00:00 0.000032 0.000137 0.000024 0.000028 However, i want to reindex the second index, by one 20min bin and I would like it to look like this: x1 x2 x3 x4 Date Time 2017-01-03 09:20:00

Resampling a Time Series in a MultiIndex DataFrame

回眸只為那壹抹淺笑 提交于 2021-01-21 09:41:34
问题 I have a hierarchical table with time series data in a MultiIndex DataFrame - see sample data below - note that Level 1 index is unevenly populated. How do I resample the DataFrame to result in a weekly time series that resamples weekly backwards from the current date. I have tried this but failed: df.index.levels[0].name = 'date' df.reset_index().groupby(pandas.Grouper(key='date', freq='W')) Sample data: Price Sector 2016-08-08 Equity(24 [AAPL]) 107.47 311 Equity(4151 [JNJ]) 124.19 206