Here is my code
A['period_id'] = A['period_number','Session'].map(B.set_index(['period_number','Session'])['period_id'])
So I want to take data from column period_id
of B to give to A, based on criteria that 2 columns ( period_number
and Session
) are matched. However it gave me error. What can I do?
You can use pd.merge
:
A_columns = A.columns
A_columns.append("period_id")
# merge based on period_number and Session
merged_df = pd.merge(A, B, how='left', left_on=['period_number','Session'], right_on = ['period_number','Session'])
final_df = merged_df[A_columns] # filter for only columns in A + `period_id` from B
Note that if A
's column names are different for period_number
and Session
, you'll have to adjust your left_on
, and vice versa for B
. To be explicit A
is the left dataframe here, and B
is the right dataframe.
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.