[英]How to Join CSV in Python Pandas Comparing 2 CSV
I have 2 csv files lets say A.csv and B.csv . 我有2个csv文件,可以说A.csv和B.csv 。 A.csv has columns a,b,c,d and B.csv has columns x,y,z,t.
A.csv具有列a,b,c,d,而B.csv具有列x,y,z,t。 I want to search that if an entry in column a exist in column x then print z and d if that rows.
我想搜索一下,如果列a中的条目存在于列x中,然后如果该行则打印z和d。
Like, 喜欢,
for each i in A
if A.[a][i] exist in B.x
print A.[d][i] + B.[z][i]
-- I have already the code below. -我已经有下面的代码了。 I just need to learn how to settle the code for this
我只需要学习如何为此解决代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
A = pd.read_csv('path1')
B = pd.read_csv('path2')
Imagine your csv data file looks like below: 想象一下,您的csv数据文件如下所示:
print(df1)
A B C D
0 1 4 7 4
1 2 5 8 5
2 3 6 9 8
print(df2
X Y Z T
0 1 11 6 8
1 5 12 8 0
2 2 13 0 4
A simple merge would solve your problem, considering Left table is df1 and Right is df2 考虑到左表是df1,右表是df2,简单的合并将解决您的问题
df = df1.merge(df2,left_on='A',right_on='X')[['Z','D']]
print(df)
Z D
0 6 4
1 0 5
This will return the matched row elements from mentioned columns. 这将返回提到的列中匹配的行元素。 (here Z,D)
(这里Z,D)
I think the simplest solution is to use a left join: 我认为最简单的解决方案是使用左联接:
>>> print(A)
a b c d
0 1 2 3 4
1 2 3 4 5
2 4 4 5 6
>>> print(B)
x y z t
0 1 20 30 40
1 3 4 5 6
>>> result = A.merge(left_on='a', right=B, right_on='x', how='left')[['z', 'd']].dropna()
>>> print(result)
z d
0 30.0 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.