python - 比较具有相同标签的 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()
。
帮助将不胜感激。谢谢!
解决方案
试试这个:
df_merge = pd.merge(ls, data, on='UserID')
您还可以使用该方法的left_on和right_on参数(熊猫合并)
推荐阅读
- android-studio - 如何在android studio中注释掉gradle.properties文件中的多行?
- php - 找不到方法。使用 Google 我的业务 API 时出错
- sql - 按另一个表排序的子查询
- android - Android手机无法安装手机上复制的apk
- python - Pandas Multi/Hierarchical Indexing - 在列之间执行算术运算
- javascript - 允许使用 JavaScript 访问网络摄像头/麦克风?
- hibernate - 一对多级联所有 - 拯救儿童和家属
- python - 处理xml中属性的空白值
- couchdb - 无法添加用户
- android - 如何使用 RecyclerView 执行缓动滚动