multi-index

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

主宰稳场 提交于 2020-12-03 11:55:31
问题 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

Pandas MultiIndex (more than 2 levels) DataFrame to Nested Dict/JSON

醉酒当歌 提交于 2020-12-02 18:32:50
问题 This question is similar to this one, but I want to take it a step further. Is it possible to extend the solution to work with more levels? Multilevel dataframes' .to_dict() method has some promising options, but most of them will return entries that are indexed by tuples (i.e. (A, 0, 0): 274.0 ) rather than nesting them in dictionaries. For an example of what I'm looking to accomplish, consider this multiindex dataframe: data = {0: { ('A', 0, 0): 274.0, ('A', 0, 1): 19.0, ('A', 1, 0): 67.0,

Pandas MultiIndex (more than 2 levels) DataFrame to Nested Dict/JSON

被刻印的时光 ゝ 提交于 2020-12-02 18:30:05
问题 This question is similar to this one, but I want to take it a step further. Is it possible to extend the solution to work with more levels? Multilevel dataframes' .to_dict() method has some promising options, but most of them will return entries that are indexed by tuples (i.e. (A, 0, 0): 274.0 ) rather than nesting them in dictionaries. For an example of what I'm looking to accomplish, consider this multiindex dataframe: data = {0: { ('A', 0, 0): 274.0, ('A', 0, 1): 19.0, ('A', 1, 0): 67.0,

missing values in pandas column multiindex

穿精又带淫゛_ 提交于 2020-07-10 07:41:08
问题 I am reading with pandas excel sheets like this one: using df = pd.read_excel('./question.xlsx', sheet_name = None, header = [0,1]) which results in multiindex dataframe with multiindex. What poses a problem here is that the empty fields are filled by default with 'Title' , whereas I would prefer to use a distinct label. I cannot skip the first row since I am dealing with bigger data frames where the first and the second rows contain repeating labels (hence the use of the multiindex). Your

pandas apply function on multiindex

有些话、适合烂在心里 提交于 2020-07-05 04:14:27
问题 I would like to apply a function on a multiindex dataframe (basically groupby describe dataframe) without using for loop to traverse level 0 index. Function I'd like to apply: def CI(x): import math sigma = x["std"] n = x["count"] return 1.96 * sigma / math.sqrt(n) Sample of my dataframe: df = df.iloc[47:52, [3,4,-1]] a b id 47 0.218182 0.000000 0d1974107c6731989c762e96def73568 48 0.000000 0.000000 0d1974107c6731989c762e96def73568 49 0.218182 0.130909 0d1974107c6731989c762e96def73568 50 0