Calculation between groups in a Pandas multiindex dataframe

xiaoxiao87

Suppose I generate a multi-index data frame as follows:

arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
          np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)

                0          1            2           3
bar one -0.155088   -0.177214   -0.761230   -0.106045
    two  1.930298   -0.309573   -0.051878   -0.388760
baz one  0.111287    1.374426    0.408575    1.555659
    two -0.809201   -0.168658    0.055037    1.871289
foo one  0.286833   -0.988538    0.918153    0.841016
    two  0.348741    0.403747    0.584992   -1.838409
qux one  1.212017   -0.224872    0.616604    1.080590
    two  0.494800   -0.089214    0.829222    2.005217

How do I create a new column, which is the ratio between group 'one' and 'two' on the their #3 column value (e.g. first element would be -0.106045 / -0.388760)?

How can I show it in conjunction with the current data frame?

Andy Hayden

With different random numbers. Use a transform:

In [11]: df.groupby(level=0)[3].transform(lambda x: x[0]/ x[1])
Out[11]:
bar  one   -1.391651
     two   -1.391651
baz  one   -1.688734
     two   -1.688734
foo  one   -1.128344
     two   -1.128344
qux  one   -2.170493
     two   -2.170493
Name: 3, dtype: float64

to show this, set it as a column:

In [12]: df["ratio"] = df.groupby(level=0)[3].transform(lambda x: x[0]/ x[1])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

concatinate pandas dataframe to multiindex

From Dev

reindex multiindex pandas dataframe

From Java

Pandas Dataframe Multiindex Merge

From Dev

Pandas multiIndex DataFrame sort

From Dev

pandas indexing in multiindex dataframe

From Dev

Merge pandas DataFrame with MultiIndex

From Dev

Building MultiIndex in Pandas DataFrame

From Dev

MultiIndex Pandas From Dataframe

From Dev

Pandas MultiIndex DataFrame Sorting

From Dev

Pandas Dataframe Multiindex Merge

From Dev

Indexing with multiindex dataframe in pandas

From Dev

Resampling a pandas MultiIndex dataframe

From Dev

Update a Pandas MultiIndex DataFrame

From Dev

Distance calculation between rows in Pandas Dataframe using a distance matrix

From Dev

Vectorized/Matrix calculation between 2 Pandas dataframe columns

From Dev

How to operate conditional calculation between columns in pandas dataframe?

From Dev

Pandas Dataframe Complex Calculation

From Dev

pandas dataframe aggregate calculation

From Dev

Efficient calculation on a pandas dataframe

From Dev

pandas dataframe aggregate calculation

From Dev

Python Pandas read_excel different behavior in parsing MultiIndex dataframe between Pandas 0.18.1 and Pandas > 0.19

From Dev

Numbering Groups In Pandas DataFrame

From Dev

Make multiindex columns in a pandas dataframe

From Java

Pandas: Collapse rows in a Multiindex dataframe

From Java

Select rows in pandas MultiIndex DataFrame

From Dev

grouping and summing multiindex dataframe in pandas

From Dev

concat MultiIndex pandas DataFrame columns

From Dev

Pandas: bar plot with multiIndex dataframe

From Dev

Reindex sublevel of pandas dataframe multiindex

Related Related

HotTag

Archive