multi-index

Join dataframes - one with multiindex columns and the other without

拟墨画扇 提交于 2021-01-21 07:37:46
问题 I'm trying to join two dataframes - one with multiindex columns and the other with a single column name. They have similar index. I get the following warning: "UserWarning: merging between different levels can give an unintended result (3 levels on the left, 1 on the right)" For example: arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] tuples = list(zip(*arrays)) index = pd.MultiIndex.from_tuples(tuples, names=[

Access last elements of inner multiindex level in pandas dataframe

可紊 提交于 2021-01-21 03:51:40
问题 In a multi index pandas dataframe I want to access the last element of the second index for all values of the first index. The number of levels in the second index vary depending on the value of the first index. I went through the pandas multi index documentation but could not find anything that does that. For example, for the data frame below: arrays = [ ['bar', 'bar', 'baz', 'foo', 'foo', 'foo', 'qux'], ['one', 'two', 'one', 'one', 'two', 'three', 'one']] tuples = list(zip(*arrays)) index =

Access last elements of inner multiindex level in pandas dataframe

喜你入骨 提交于 2021-01-21 03:51:03
问题 In a multi index pandas dataframe I want to access the last element of the second index for all values of the first index. The number of levels in the second index vary depending on the value of the first index. I went through the pandas multi index documentation but could not find anything that does that. For example, for the data frame below: arrays = [ ['bar', 'bar', 'baz', 'foo', 'foo', 'foo', 'qux'], ['one', 'two', 'one', 'one', 'two', 'three', 'one']] tuples = list(zip(*arrays)) index =

Flatten a single-leveled multiIndex

两盒软妹~` 提交于 2021-01-07 02:21:52
问题 Indexing into a Pandas DataFrame throws an error when: The query has multiple copies of the same value. The index in the DataFrame is a single-level MultiIndex. The below code shows a simple example import pandas as pd # columns as str class -- works D = pd.DataFrame([[1,2]], columns = ['A','B'], index = ['R1']) print(D.loc[:,['A','A']], '\n') # OK # columns as Index class -- works D = pd.DataFrame([[1,2]], columns = pd.Index(['A','B']), index = ['R1']) print(D.loc[:,['A','A']], '\n') # OK #

Flatten a single-leveled multiIndex

自作多情 提交于 2021-01-07 02:21:28
问题 Indexing into a Pandas DataFrame throws an error when: The query has multiple copies of the same value. The index in the DataFrame is a single-level MultiIndex. The below code shows a simple example import pandas as pd # columns as str class -- works D = pd.DataFrame([[1,2]], columns = ['A','B'], index = ['R1']) print(D.loc[:,['A','A']], '\n') # OK # columns as Index class -- works D = pd.DataFrame([[1,2]], columns = pd.Index(['A','B']), index = ['R1']) print(D.loc[:,['A','A']], '\n') # OK #

Flatten a single-leveled multiIndex

橙三吉。 提交于 2021-01-07 02:21:16
问题 Indexing into a Pandas DataFrame throws an error when: The query has multiple copies of the same value. The index in the DataFrame is a single-level MultiIndex. The below code shows a simple example import pandas as pd # columns as str class -- works D = pd.DataFrame([[1,2]], columns = ['A','B'], index = ['R1']) print(D.loc[:,['A','A']], '\n') # OK # columns as Index class -- works D = pd.DataFrame([[1,2]], columns = pd.Index(['A','B']), index = ['R1']) print(D.loc[:,['A','A']], '\n') # OK #

Faster way to make pandas Multiindex dataframe than append

江枫思渺然 提交于 2021-01-07 01:34:52
问题 I am looking for a faster way to load data from my json object into a multiindex dataframe. My JSON is like: { "1990-1991": { "Cleveland": { "salary": "$14,403,000", "players": { "Hot Rod Williams": "$3,785,000", "Danny Ferry": "$2,640,000", "Mark Price": "$1,400,000", "Brad Daugherty": "$1,320,000", "Larry Nance": "$1,260,000", "Chucky Brown": "$630,000", "Steve Kerr": "$548,000", "Derrick Chievous": "$525,000", "Winston Bennett": "$525,000", "John Morton": "$350,000", "Milos Babic": "$200

How to join a multi-index series to a single index dataframe with Pandas?

家住魔仙堡 提交于 2020-12-03 11:59:29
问题 Consider the following single index DataFrame: energy fat 1 2000 28 2 1900 17 3 2200 30 4 1750 15 5 1800 18 6 1600 12 I also have a multindex Series: 1 vitamin-c 0.0004 vitamin-a 0.0150 2 vitamin-c 0.0030 3 vitamin-d 1.2000 vitamin-e 1.0007 vitamin-c 1.2020 4 vitamin-a 0.0780 5 vitamin-b 0.9650 6 vitamin-e 1.9801 vitamin-c 1.0011 How can I join the two so the result looks like this: energy fat vitamins 1 2000 28 vitamin-c 0.0004 vitamin-a 0.0150 2 1900 17 vitamin-c 0.0030 3 2200 30 vitamin-d

How to join a multi-index series to a single index dataframe with Pandas?

寵の児 提交于 2020-12-03 11:55:46
问题 Consider the following single index DataFrame: energy fat 1 2000 28 2 1900 17 3 2200 30 4 1750 15 5 1800 18 6 1600 12 I also have a multindex Series: 1 vitamin-c 0.0004 vitamin-a 0.0150 2 vitamin-c 0.0030 3 vitamin-d 1.2000 vitamin-e 1.0007 vitamin-c 1.2020 4 vitamin-a 0.0780 5 vitamin-b 0.9650 6 vitamin-e 1.9801 vitamin-c 1.0011 How can I join the two so the result looks like this: energy fat vitamins 1 2000 28 vitamin-c 0.0004 vitamin-a 0.0150 2 1900 17 vitamin-c 0.0030 3 2200 30 vitamin-d

How to join a multi-index series to a single index dataframe with Pandas?

倖福魔咒の 提交于 2020-12-03 11:55:43
问题 Consider the following single index DataFrame: energy fat 1 2000 28 2 1900 17 3 2200 30 4 1750 15 5 1800 18 6 1600 12 I also have a multindex Series: 1 vitamin-c 0.0004 vitamin-a 0.0150 2 vitamin-c 0.0030 3 vitamin-d 1.2000 vitamin-e 1.0007 vitamin-c 1.2020 4 vitamin-a 0.0780 5 vitamin-b 0.9650 6 vitamin-e 1.9801 vitamin-c 1.0011 How can I join the two so the result looks like this: energy fat vitamins 1 2000 28 vitamin-c 0.0004 vitamin-a 0.0150 2 1900 17 vitamin-c 0.0030 3 2200 30 vitamin-d