Map values on DataFrame by two columns

时间:2016-03-04 17:49:04

标签: python python-2.7 pandas

Suppose I am trying to map total_cases_age onto cases_by_age where dataframes are:

 results_grouped_age = results_grouped[['Make', 'age', 'Test Result', 'Number of Cases']].copy()
    cases_by_age = results_grouped_age[['Make','age','Test Result','Number of Cases']].groupby(['Make','age','Test Result']).sum().reset_index()
    total_cases_age = cases_by_age.groupby(['Make','age'])['Number of Cases'].sum()

However whereas I would normally do:

cases_by_age['Total Cases'] = cases_by_age['age'].map(total_cases_age)

Indices of total_cases_age is actually a combination of 'make and age' and this is actually what I want to do. To easier understand my problem, suppose I have table cases_by_age"

        Make      age     Test Result     Number of Cases
0    ALFA ROMEO   0-3         ABA                1
1    ALFA ROMEO   0-3         ABR              NaN
2    ALFA ROMEO   0-3           F               45
3    ALFA ROMEO   0-3           P              268
4    ALFA ROMEO   0-3         PRS               21
5    ALFA ROMEO   3-5         ABA              NaN
6    ALFA ROMEO   3-5         ABR              NaN
7    ALFA ROMEO   3-5           F              159
8    ALFA ROMEO   3-5           P              720

And the end result should be something like this:

        Make      age     Test Result     Number of Cases     Total Cases by Age
0    ALFA ROMEO   0-3         ABA                1                 335
1    ALFA ROMEO   0-3         ABR              NaN                 335 
2    ALFA ROMEO   0-3           F               45                 335
3    ALFA ROMEO   0-3           P              268                 335
4    ALFA ROMEO   0-3         PRS               21                 335
5    ALFA ROMEO   3-5         ABA              NaN                 879
6    ALFA ROMEO   3-5         ABR              NaN                 879
7    ALFA ROMEO   3-5           F              159                 879
8    ALFA ROMEO   3-5           P              720                 879

And so on for makes and ages

Any help will be greatly appreciated

1 个答案:

答案 0 :(得分:1)

You could do a groupby-sum, followed by a left-merge:

pd.merge(
    df,
    df['Number of Cases'].groupby(df['age']).sum().reset_index().rename(
        columns={'Number of Cases': 'Total Cases by Age'}),
    how='left')

Example

Suppose you start with

df = pd.DataFrame({
    'Make': ['ALPHA ROMEO'] * 3,
    'age': ['0-3', '0-3', '3-5'],
    'Number of Cases': [1, 10, 2]
    })
>>> df
    Make    Number of Cases age
0   ALPHA ROMEO 1   0-3
1   ALPHA ROMEO 10  0-3
2   ALPHA ROMEO 2   3-5

Then the groupby-sum gives:

>>> df['Number of Cases'].groupby(df['age']).sum().reset_index().rename(
    columns={'Number of Cases': 'Total Cases by Age'})
    age Total Cases by Age
0   0-3 11
1   3-5 2

And the combination gives:

>>> pd.merge(
    df,
    df['Number of Cases'].groupby(df['age']).sum().reset_index().rename(
        columns={'Number of Cases': 'Total Cases by Age'}),
    how='left')
    Make    Number of Cases age Total Cases by Age
0   ALPHA ROMEO 1   0-3 11
1   ALPHA ROMEO 10  0-3 11
2   ALPHA ROMEO 2   3-5 2