python - 无法从另一个数据框中创建数据框中的列
问题描述
我正在尝试通过将值与其他数据框匹配来在现有数据框中创建一个新列。
数据框 1 - (电影)
index | rating | movie_id | movie_title
--------------------------------------------
0 | 5 | 100 | Inception
1 | 4 | 101 | Starwars
数据框 2 - (建议)
index | rating | movie_id
------------------------------
0 | 3.9 | 101
1 | 4.7 | 100
我想要的——(推荐):
index | rating | movie_id | movie_title
--------------------------------------------
0 | 3.9 | 101 | Starwars
1 | 4.7 | 100 | Inception
我试图做的事情:(虽然这没有意义)
pd.merge(movies , recommendations, on ='movie_id', how ='left')
这没有意义,因为两个数据帧的大小不同。推荐数据框的大小由用户通过控制台给出。
解决方案
你可以试试这个:
newdf=pd.merge(recommendations,movies[movies.columns[1:]], how='left',on='movie_id')
print(newdf)
输出:
rating movie_id movie_title
0 3.9 101 Starwars
1 4.7 100 Inception
推荐阅读
- node.js - 将 nodejs typescript 项目构建到生产环境
- visual-studio-code - 有没有办法在 VS Code 中复制没有缩进的行?
- java - DuplicateMappingException 表[]包含多个逻辑列名引用的物理列名[]:[_id]、[Id]
- python-3.x - Jupyter 笔记本无法正常执行
- c++ - 多参数函数上的 std::bind 在函数模板中的编译时解析失败
- php - 我无法从 SQL 查询中读取数据?
- bash - 如何在 shellscript 的条件下测试动态子字符串变量?
- java - 创建排除引号内容的空格分隔列表
- java - 如何根据java 8中的某些条件迭代和过滤列表?
- c - 在变量初始化上强制转换指向 Matrix 的指针是引用或副本