python - How to perform cross check between to different length columns in dataframes and create an a new dataframe?
问题描述
I have two dataframes,
df1:
pno | bno | report
1 | 12 | somereport2.pdf
11 | 12 | somereporter.pdf
12 | 12 | somereportf.pdf
11 | 12 | somereportwee.pdf
1 | 12 | somereport22.pdf
11 | 12 | somereport22.pdf
df2:
pno
11
12
I want to create a new df based on a column pno
of df1 and df2. So df3:
pno | bno | report
11 | 12 | somereporter.pdf
12 | 12 | somereportf.pdf
11 | 12 | somereportwee.pdf
11 | 12 | somereport22.pdf
That is the new df will only have values that are in df2 pno
column. I tried using merge
function as
newdf = pd.merge(df1, df2, how="inner", on=["pno","pno"]
But it created some random shape with lot of missing values. I tried to do left join,
newdf = pd.merge(df1, df2, how="left", on=["pno","pno"]
But it kept all the values without cross checking.
Is there a way to crosscheck one column with another and only keep those values in new df?
解决方案
Use isin to filter and mask the rowms you want
df1[df1['pno'].isin(df2['pno'])]
推荐阅读
- python - 如何从 configparser config.ini 文件中获取带有重音的特殊字符的字符串?
- javascript - 如果选中组合单选按钮,如何显示不同的图像?
- import - 从 Mysql DB 导入 Sqoop 需要大量时间才能完成
- php - XAMPP Virtualhost 不路由
- php - 我可以引用之前创建的函数吗?
- c - 使用递归函数将十进制转换为二进制
- css - 网格列响应宽度,但不大于不小于
- react-testing-library - 使用 inside 和自定义查询的反应测试库
- bash - 如何在 Google Colab 中实现这些 bash 命令
- php - React - PHP:如何通过 POST 请求的获取请求解决 CORS 问题?