首页 > 解决方案 > 比较具有相同标签的 Series 对象

问题描述

我想使用一个数据集中的数据来过滤另一个数据集中的数据。我的数据看起来像:

ls.head()
UserID  Rating  ISBN13  GoodreadsID     Title   Author
18266   2146832     5   0060525592  275000  Fire and Ice (Warriors, #2)     Erin Hunter
18267   2119385     5   0060525592  275000  Fire and Ice (Warriors, #2)     Erin Hunter
18272   2117173     5   0060525592  275000  Fire and Ice (Warriors, #2)     Erin Hunter
18273   2117009     5   0060525592  275000  Fire and Ice (Warriors, #2)     Erin Hunter
18274   2106234     5   0060525592  275000  Fire and Ice (Warriors, #2)     Erin Hunter

和我的第二个数据集:

data.head()

    UserID  Rating  ISBN13  GoodreadsID     Title   Author
150834  2131509     5   0143038419  19501   Eat, Pray, Love     Elizabeth Gilbert
59347   2113561     5   0374528373  4934    The Brothers Karamazov  Fyodor Dostoyevsky
37087   2122197     5   0316015849  41865   Twilight (Twilight, #1)     Stephenie Meyer
950201  2107691     3   044619817X  5931169     Santa Olivia (Santa Olivia, #1)     Jacqueline Carey
114404  2144218     3   0441015891  2233407     From Dead to Worse (Sookie Stackhouse, #8)  Charlaine Harris
1053208     2143953     4   0451463099  6582703     Unknown (Outcast Season, #2)    Rachel Caine
18290   2148946     5   0060525592  275000  Fire and Ice (Warriors, #2)     Erin Hunter
1585865     2140143     3   1594742812  4133366     The Curious Case of Benjamin Button: A Graphic...   Nunzio DeFilippis
1115470     2125069     0   0758234937  11796251    Cinnamon Roll Murder (Hannah Swensen, #15)  Joanne Fluke
484235  2108848     5   0553816713  15931   The Notebook (The Notebook, #1)     Nicholas Sparks

我想使用列 ls['UserID'] 并从第二个数据集('data')中选择所有这些用户。

我试过了:

data.loc[ls['UserID'] == data['UserID']]

...这给了我

ValueError:只能比较标签相同的系列对象

我都试过了sort_index()reset_index()

帮助将不胜感激。谢谢!

标签: pythonpandasnumpy

解决方案


试试这个:

df_merge = pd.merge(ls, data, on='UserID')

您还可以使用该方法的left_onright_on参数(熊猫合并)


推荐阅读