Python / Pandas - Filtering according to other dataframe's index -
i have 2 dataframes:
df1: value dude_id 123 x 543 y 984 z df2: value id 123 r 498 s 543 d 984 x 009 z
i want filter df2
in way contains keys present in df1
's index. should this:
df2: value id 123 r 543 d 984 x
i tried following:
df2.filter(like=df.index, axis=0)
however taking me following error:
valueerror: truth value of int64index ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
what missing?
use loc
in [952]: df2.loc[df1.index] out[952]: value dude_id 123 r 543 d 984 x
and, can rename index name
in [956]: df2.loc[df1.index].rename_axis('id') out[956]: value id 123 r 543 d 984 x
Comments
Post a Comment