[Python] Multiply the value determined for each section

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I am in a situation where I need to multiply several columns by the value corresponding to the interval.

    I'm using Pandas, but I'm using a for loop to get the value as follows.

    I think there is a better way than this, can someone give me some advice?

    sample:

    import pandas as pd

    d = {
    5: 5,
    4: 7,
    3: 2,
    2: -4,
    1: -5,
    }
    def mul(value):
    if value < 1: return value * 0.01
    elif 5 < value: return value * 20
    for k, v in d.items():
    if value <= k:
    val = value * v
    break
    return val

    a = [
    {'a': 5, 'b': 10, 'c': 2},
    {'a': 4, 'b': 0.5, 'c': 4},
    {'a': 3.5, 'b': 1.5, 'c': 5},
    {'a': 2.1, 'b': 5, 'c': 6},
    {'a': 0.1, 'b': 1, 'c': 7},
    ]

    df = pd.DataFrame(a)
    a = []
    b = []
    for i in df.values:
    a.append(mul(i[0]))
    b.append(mul(i[1]))
    df['a1'] = a
    df['b1'] = b
    print(df.head())

    """
    a b c a1 b1
    0 5.0 10.0 2.0 25.000 200.000
    1 4.0 0.5 4.0 20.000 0.005
    2 3.5 1.5 5.0 17.500 7.500
    3 2.1 5.0 6.0 10.500 25.000
    4 0.1 1.0 7.0 0.001 5.000
    """

    Continue reading...

Compartilhe esta Página