Wednesday, August 1, 2018

Advanced Indexing from Multi-Index/Pivoted Dataframe

In this tutorial we will learn how to select row x column  in python multi-level dataframe using loc() function .  Lets see with an example

Consider the following data:


d = {
    'app' : ['J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'B'],
    'geo':
        ['US', 'US', 'US', 'Asia', 'Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'US'],
    'device':
        ['iPhone', 'iPad', 'Android', 'iPhone', 'iPad', 'Android', 'iPhone', 'iPad', 'Android', 'iPhone'],
    'cost':
        [4500, 4000, 4500, 2000, 2000, 2500, 500, 500, 500, 250]}


df = pd.DataFrame(d)





















and a pivot out of it


df_pivoted =  pd.pivot_table(df, index = 'app', columns=['geo', 'device'], values=['cost'], aggfunc=np.sum)

and you want to select the cost under row 'B' with geo 'Asia'


df.loc[['J'], ('cost', 'Asia')]




0 comments:

Post a Comment