Reputation: 57
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 DataFrameSee 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
Upvotes: 1
Views: 527
Reputation: 8703
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):
Upvotes: 1
Reputation: 863501
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
Upvotes: 2