简体   繁体   中英

How to replace values in a dataframe with values in another dataframe based on certain condition?

I want to replace the values of columns "q1_body" and "q2_body" of dataframe "result" with the values of "body" of the same id in dataframe "df", and the code is like:

def replace_body(x):
    id1 = result.loc[x].qid1
    result.loc[x].q1_body = df[df["qid"]==id1]["body"]
    id2 = result.loc[x].qid2
    result.loc[x].q2_body = df[df["qid"]==id2]["body"]

result.index.map(lambda x: replace_body(x))

When I run the code I got the following reminder in my ipython console and the program just stuck here:

//anaconda/lib/python3.6/site-packages/pandas/core/generic.py:3110:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self[name] = value

Hope anyone can tell me what is wrong here.

Suppose the two dataframe are:

result:

qid1 q1_body qid2 q2_body
 1a    abc    2a    bcd
 1a    abc    3a    cde
 2a    bcd    3a    cde

df:

qid body
1a sfgaks
2a shdfjk
3a adjkwf

And the expected output is like:

result:

qid1 q1_body qid2 q2_body
 1a  sfgaks   2a  shdfjk
 1a  sfgaks   3a  adjkwf
 2a  shdfjk   3a  adjkwf

You need map by Series created by set_index :

s = df.set_index('qid')['body']
result['q1_body'] = result['qid1'].map(s)
result['q2_body'] = result['qid2'].map(s)
print (result)
  qid1 q1_body qid2 q2_body
0   1a  sfgaks   2a  shdfjk
1   1a  sfgaks   3a  adjkwf
2   2a  shdfjk   3a  adjkwf

Here:

# Set index and get body as a series
s = df.set_index(qid)['body']
result['q1_body'] = s.loc[result['qid1']].values
result['q2_body'] = s.loc[result['qid2']].values

Result:

  qid1 q1_body qid2 q2_body
0   1a  sfgaks   2a  shdfjk
1   1a  sfgaks   3a  adjkwf
2   2a  shdfjk   3a  adjkwf

Timing (10k rows, using auto-generated Lorem):

My method 我的方法

@Jezareal's method @ Jezreal的方法

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