Home > Enterprise >  replacing values in a data frame from a dictionary with multiple keys
replacing values in a data frame from a dictionary with multiple keys

Time:11-01

I have not seen any posts about this on here. I have a data frame with some data that i would like to replace the values with the values found in a dictionary. This could simply be done with .replace but I want to keep this dynamic and reference the df column names using a paired dictionary map.

import pandas as pd

data=[['Alphabet', 'Indiana']]
df=pd.DataFrame(data,columns=['letters','State'])

replace_dict={
    "states":
        {"Illinois": "IL", "Indiana": "IN"},

    "abc":
        {"Alphabet":"ABC", "Alphabet end":"XYZ"}}

def replace_dict():
    return

df_map={
"letters": [replace_dict],
"State": [replace_dict]
}

#replace the df values with the replace_dict values

I hope this makes sense but to explain more i want to replace the data under columns 'letters' and 'State' with the values found in replace_dict but referencing the column names from the keys found in df_map. I know this is overcomplicated for this example but i want to provide an easier example to understand.

I need help making the function 'replace_dict' to do the operations above.

Expected output is:

data=[['ABC', 'IN']]
df=pd.DataFrame(data,columns=['letters','State'])

by creating a function and then running the function with something along these lines

for i in df_map:
for j in df_map[i]:
df= j(i, df)

how would i create a function to run these operations? I have not seen anyone try to do this with multiple dictionary keys in the replace_dict

CodePudding user response:

I'd keep the replace_dict keys the same as the column names.

import pandas as pd


def map_from_dict(data: pd.DataFrame, cols: list, mapping: dict) -> pd.DataFrame:
    dfs = []
    for col in cols:
        data[col] = data[col].map(mapping.get(col))
        dfs.append(df)

    return pd.concat(dfs).drop_duplicates(keep="first")


df = pd.DataFrame({
    "letters": ["Alphabet"],
    "states": ["Indiana"]
})

replace_dict = {
    "states": {"Illinois": "IL", "Indiana": "IN"},
    "letters": {"Alphabet": "ABC", "Alphabet end": "XYZ"}
}

final_df = map_from_dict(df, ["states", "letters"], replace_dict)
print(final_df)

  letters states
0     ABC     IN

CodePudding user response:

import pandas as pd

data=[['Alphabet', 'Indiana']]
df=pd.DataFrame(data,columns=['letters','State'])

dict_={
    "states":
        {"Illinois": "IL", "Indiana": "IN"},

    "abc":
        {"Alphabet":"ABC", "Alphabet end":"XYZ"}}

def replace_dict(df, dict_):
    for d in dict_.values():
        for val in d:
            for c in df.columns:
                df[c][df[c]==val] = d[val]
    return df

df = replace_dict(df, dict_)
  • Related