1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Get subset of dataframe following a condition from a dictionary

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to find instances of where three columns may exceed some number that is stored in a dictionary. I know this code works, but I think there is a better more pythonic way of doing this.

    l = []
    for index, row in df_usage.iterrows():
    item_id = row['item_id']
    try:
    if (
    row['start_count'] > historical_orders_dict[item_id]
    or row['end_count'] > historical_orders_dict[item_id]
    or row['received_total'] > historical_orders_dict[item_id]
    ):
    l.append(df_usage.loc[[index], :])
    except:
    pass
    df_too_many_consuables = pd.concat(l).reset_index(drop=True)
    df_too_many_consuables.shape


    Here is an example

    historical_orders_dict = {
    '10346': 28644.99,
    '10877': 28979.99,
    '10695': 6200.0,
    '70020': 1960.0,
    '40265': 57300.0,
    '91524': 9750.0,
    '60022': 200.0,
    '10210': 156.0,
    '11040': 49350.0}

    data = {
    'item_id': ['10346', '10877', '10695', '70020', '40265', '91524',
    '60022', '10210','11040'],
    'start_count': [100000000, 2, 3, 4, 5, 6, 7, 8, 9],
    'end_count': [10, 11, 12, 13, 14, 15, 16, 17, 18],
    'received_total': [19, 20, 21, 22, 23, 24, 46, 45, 34]}
    df_usage = pd.DataFrame(data=data)

    Continue reading...

Compartilhe esta Página