如何将数据附加到 Pandas 多索引数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30921736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append data to pandas multi-index dataframe
提问by Aran Freel
How can I append data to a Pandas Multi-Index DataFrame? I currently use the following code to successfully create a dataframe from my data.
如何将数据附加到 Pandas 多索引数据帧?我目前使用以下代码从我的数据成功创建一个数据框。
df = pd.DataFrame.from_dict(output, orient='index')
I am thinking maybe something like this...
我在想也许是这样的......
df = pd.DataFrame['MMM', 'IncomeStatement'].from_dict(output, orient='index')
DataFrame to merge
要合并的数据帧
0 1 2
Total Revenue 182795000 170910000 156508000
Cost of Revenue 112258000 106606000 87846000
Gross Profit 70537000 64304000 68662000
Research Development 6041000 4475000 3381000
Selling General and Administrative 11993000 10830000 10040000
Non Recurring 0 0 0
Others 0 0 0
Total Operating Expenses 0 0 0
Operating Income or Loss 52503000 48999000 55241000
Total Other Income/Expenses Net 980000 1156000 522000
Earnings Before Interest And Taxes 53483000 50155000 55763000
Interest Expense 0 0 0
Income Before Tax 53483000 50155000 55763000
Income Tax Expense 13973000 13118000 14030000
Minority Interest 0 0 0
Net Income From Continuing Ops 39510000 37037000 41733000
Discontinued Operations 0 0 0
Extraordinary Items 0 0 0
Effect Of Accounting Changes 0 0 0
Other Items 0 0 0
Net Income 39510000 37037000 41733000
Preferred Stock And Other Adjustments 0 0 0
Net Income Applicable To Common Shares 39510000 37037000 41733000
Multi-Index / Parent DataFrame
多索引/父数据帧
MMM IncomeStatemen
BalanceSheet
CashFlows
ABT IncomeStatement
BalanceSheet
CashFlows
ABBV IncomeStatement
BalanceSheet
CashFlows
ACN IncomeStatement
BalanceSheet
CashFlows
Result
结果
MMM IncomeStatement Total Revenue 182795000 170910000 156508000
Cost of Revenue 112258000 106606000 87846000
Gross Profit 70537000 64304000 68662000
Research Development 6041000 4475000 3381000
Selling General and Administrative 11993000 10830000 10040000
Non Recurring 0 0 0
Others 0 0 0
Total Operating Expenses 0 0 0
Operating Income or Loss 52503000 48999000 55241000
Total Other Income/Expenses Net 980000 1156000 522000
Earnings Before Interest And Taxes 53483000 50155000 55763000
Interest Expense 0 0 0
Income Before Tax 53483000 50155000 55763000
Income Tax Expense 13973000 13118000 14030000
Minority Interest 0 0 0
Net Income From Continuing Ops 39510000 37037000 41733000
Discontinued Operations 0 0 0
Extraordinary Items 0 0 0
Effect Of Accounting Changes 0 0 0
Other Items 0 0 0
Net Income 39510000 37037000 41733000
Preferred Stock And Other Adjustments 0 0 0
Net Income Applicable To Common Shares 39510000 37037000 41733000
BalanceSheet
CashFlows
ABT IncomeStatement
BalanceSheet
CashFlows
ABBV IncomeStatement
BalanceSheet
CashFlows
ACN IncomeStatement
BalanceSheet
CashFlows
采纳答案by Ami Tavory
Am using simplified versions of your DataFrames.
我正在使用您的 DataFrame 的简化版本。
Suppose you start with:
假设你开始:
import pandas as pd
import numpy as np
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
s = pd.DataFrame(index=arrays)
so that
以便
>> s
bar one
two
baz one
two
foo one
two
qux one
two
(this is your parent)
(这是你的父母)
and also
并且
c = pd.DataFrame(index=['one', 'two'], data=[23, 33])
so that
以便
>> c
0
one 23
two 33
(this is your first DataFrame)
(这是你的第一个 DataFrame)
So, a merge+ groupbygive
所以,一个merge+groupby给
>> pd.merge(s.reset_index(), c, left_on='level_1', right_index=True).groupby(['level_0', 'level_1']).sum()
0
level_0 level_1
bar one 23
two 33
baz one 23
two 33
foo one 23
two 33
qux one 23
two 33

