pythonpandasdataframedictionary

Creating a column based on column values and dictionaries using vectorization in pandas


I have three columns in a pandas dataframe A, B, and C. I also have 3 dictionaries, dict_A, dict_B, dict_C that have all values of A, B, and C as keys in each respective dataframe. I am trying to create 2 new columns in the dataframe that use the data in these dictionaries as part of a calculation. This is what I tried

df['weight_A'] = (dict_A[df['A']] * dict_C[df['C']])
df['weight_B'] = (dict_B[df['B']] * (1 - dict_C[df['C']]))

I can't do this because I cannot use the series as a key to the dataframe. I know I can iterate over the rows, but want to do it vectorized.


Solution

  • You can use map:

    df["weight_A"] = df["A"].map(dict_A) * df["C"].map(dict_C)
    df["weight_B"] = df["B"].map(dict_B) * (1 - df["C"].map(dict_C))