简体   繁体   English

Python大熊猫从左表获取行,并从左表获取右表缺少的行

[英]Python pandas get rows from left table and from right table missing in left table

I have left and right table and I need to merge FileStamp values from both in this manner: take all values from left table and from right table missing in left table, join it by 'date': 我有左右表,我需要以这种方式合并两个表的FileStamp值:取左表和左表中缺少的右表中的所有值,并按'date'联接:

import pandas as pd
left = pd.DataFrame({'FileStamp': ['T101', 'T102', 'T103', 'T104'], 'date': [20180101, 20180102, 20180103, 20180104]})
right = pd.DataFrame({'FileStamp': ['T501', 'T502'], 'date': [20180104, 20180105]})

Something like 就像是

result = pd.merge(left, right, how='outer', on='date')

but 'outer' is not good idea. 但是“外面”不是一个好主意。

Desired output should look like 所需的输出应如下所示

     FileStamp_x      date      FileStamp_y
0        T101       20180101         NaN
1        T102       20180102         NaN
2        T103       20180103         NaN
3        T104       20180104         NaN
4         NaN       20180105        T502

Is there any simple way how to achieve desired output? 有什么简单的方法可以实现所需的输出吗?

Use filtering by isin before merge : merge之前使用isin进行过滤:

r = right[~right['date'].isin(left['date'])]
print (r)
  FileStamp      date
1      T502  20180105

result = pd.merge(left, r, how='outer', on='date')
print (result)
  FileStamp_x      date FileStamp_y
0        T101  20180101         NaN
1        T102  20180102         NaN
2        T103  20180103         NaN
3        T104  20180104         NaN
4         NaN  20180105        T502

You can adjust the values after the merge : 您可以在merge后调整值:

result = pd.merge(left, right, how='outer', on='date')
result['FileStamp_y'] = np.where(result['FileStamp_x'].isnull(), result['FileStamp_y'], np.nan)

Result: 结果:

    FileStamp_x     date  FileStamp_y
0          T101 20180101          NaN
1          T102 20180102          NaN
2          T103 20180103          NaN
3          T104 20180104          NaN
4           NaN 20180105         T502

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过从左表中获取所有行并仅在右表中匹配来连接 2 个表 - How to join 2 tables with getting all rows from left table and only matching ones in right 通过从右表中采样为左联接填写NaN值 - Fill in NaN values for left join by sampling from right table 从 json 数据创建一个表状结构,左侧键和表右侧值,并使用 python Z31976206A7D4BA9 在 html 上显示 - Create a table like structure with keys on the left and values on the right of the table from json data and display on html using python flask 在Pandas DataFrame中,如何合并/合并两个具有左表中所有行的DataFrame,并从右DataFrame中重复值 - In Pandas DataFrame how to merge/join two DataFrame that has all row from left table and repeat values from right DataFrame Pandas 左连接是右表上的 NA 值作为通配符 - Pandas left join were NA values on right table are taken as a wildcard pandas dataframe是从右到左? - pandas dataframe is from the right to the left? 从两个在 Pandas 中缺少行的表中创建最完整的表 - Create the most complete table from two tables with missing rows in pandas 熊猫左外连接导致表大于左表 - Pandas Left Outer Join results in table larger than left table 使用Pandas的左联接表(1:n),行数与左表相同 - Left join tables (1:n) using Pandas, keeping number of rows the same as left table Pandas:从左到右和从右到左交替 iterrows() - Pandas: Alternating iterrows() from left to right and right to left
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM