简体   繁体   中英

Replace all values in pandas dataframe by lookup from another dataframe

Given the following data:

df = pd.DataFrame(
    dict(
        x1=["zero", "one", "two"],
        x2=["one", "zero", "zero"],
        x3=["zero", "two", "one"],
        x4=["zero", "two", "two"],
    )
)

which looks as:

In [2]: df
Out[2]:
     x1    x2    x3    x4
0  zero   one  zero  zero
1   one  zero   two   two
2   two  zero   one   two

I would like to replace elements within it using the following:

reps = pd.DataFrame(
    dict(
        val_from=["zero", "one", "two"],
        val_to=["nothing", "single", "couple"],
    )
)

resulting in the following data:

    x1    : x2     : x3     : x4
  single  : single : single : single
   single : single : couple : couple
   couple : single : single : couple

The following works, but I feel there's a better approach:

replacement = {x:y for x,y in zip(reps['val_from'], reps['val_to'])}
df.transform(lambda x: x.replace(replacement))

You could use DataFrame.replace passing the Series with index "from" and values "to" from reps :

df.replace(reps.set_index(['val_from'])['val_to'])

[out]

        x1       x2       x3       x4
0  nothing   single  nothing  nothing
1   single  nothing   couple   couple
2   couple  nothing   single   couple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM