How to append data to pandas multi-index dataframe -
how can append data pandas multi-index dataframe? use following code create dataframe data.
df = pd.dataframe.from_dict(output, orient='index')
i thinking maybe this...
df = pd.dataframe['mmm', 'incomestatement'].from_dict(output, orient='index')
dataframe 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 , 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 , 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 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 , other adjustments 0 0 0 net income applicable 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 , 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 , 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 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 , other adjustments 0 0 0 net income applicable common shares 39510000 37037000 41733000 balancesheet cashflows abt incomestatement balancesheet cashflows abbv incomestatement balancesheet cashflows acn incomestatement balancesheet cashflows
am using simplified versions of dataframes.
suppose start with:
import pandas pd import numpy 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 1 2 baz 1 2 foo 1 2 qux 1 2
(this parent)
and also
c = pd.dataframe(index=['one', 'two'], data=[23, 33])
so
>> c 0 1 23 2 33
(this first dataframe)
so, merge
+ groupby
give
>> 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 1 23 2 33 baz 1 23 2 33 foo 1 23 2 33 qux 1 23 2 33
Comments
Post a Comment