python pandas multi-level index getting a particular value

76 views Asked by At

i have a pandas dataframe of grouped values every hour

2020.05.05 17:00:00  cpu_usage_specint        4.354603
                     phys_iops             3075.779680
                     total_memory         13843.480000
2020.05.05 18:00:00  cpu_usage_specint        3.670237
                     phys_iops             3403.007340
                                              ...     
2020.05.11 22:00:00  phys_iops            30459.577340
                     total_memory         28851.025000
2020.05.11 23:00:00  cpu_usage_specint        1.903852
                     phys_iops             8161.811320
                     total_memory         28827.575000

the index is a MultiIndex([('2020.05.05 17:00:00', 'cpu_usage_specint'),
            ('2020.05.05 17:00:00',         'phys_iops'),
            ('2020.05.05 17:00:00',      'total_memory'),
            ('2020.05.05 18:00:00', 'cpu_usage_specint'),
            ('2020.05.05 18:00:00',         'phys_iops'),
            ('2020.05.05 18:00:00',      'total_memory'),
            ('2020.05.05 19:00:00', 'cpu_usage_specint'),
            ('2020.05.05 19:00:00',         'phys_iops'),
            ('2020.05.05 19:00:00',      'total_memory'),
            ('2020.05.05 20:00:00', 'cpu_usage_specint'),
            ...
            ('2020.05.11 20:00:00',      'total_memory'),
            ('2020.05.11 21:00:00', 'cpu_usage_specint'),
            ('2020.05.11 21:00:00',         'phys_iops'),
            ('2020.05.11 21:00:00',      'total_memory'),
            ('2020.05.11 22:00:00', 'cpu_usage_specint'),
            ('2020.05.11 22:00:00',         'phys_iops'),
            ('2020.05.11 22:00:00',      'total_memory'),
            ('2020.05.11 23:00:00', 'cpu_usage_specint'),
            ('2020.05.11 23:00:00',         'phys_iops'),
            ('2020.05.11 23:00:00',      'total_memory')],

i want the specific value for each metric for each hour for example

for date in metrics_pivot_df.index.get_level_values('date').unique():
    print('this is the date',date)

which gives me the date but i want the value for say total_memory too so i can do some tests such as >= etc however i dunno how to get the value? i have tried ref the column 'mertic' but this comes back true/false when i try and do a query

any help much appreciated

1

There are 1 answers

0
higgytech On

i couldn't see the wood for the trees

for date in metrics_pivot_df.index.get_level_values('date').unique():
    print('this is a specific date',date, metrics_pivot_df.loc[date, 'cpu_usage_specint'])

solved it