[英]Get matching rows in two different dataframe python
Thanks in advance- the problem is to compare rows of two separate dataframes of csv files;在此先感谢 - 问题是比较 csv 文件的两个单独数据帧的行; with and without column headings.
有和没有列标题。 I want to match rows in second dataframe to rows in dataframe one.
我想将第二个 dataframe 中的行与第一个 dataframe 中的行匹配。 I cannot use merge because both don't have common column names to merge with.
我不能使用合并,因为两者都没有要合并的通用列名。
1: The first dataframe have headings 1:第一个dataframe有标题
2: Second dataframe is without headings. 2:第二个 dataframe 没有标题。
3: get the position of the match 3:得到匹配的position
I have tried this:我试过这个:
df1 = pd.read_csv(data1)
df2 = pd.read_csv(data2)
def test1():
for index, rows in df1.iterrows():
if rows in (df2):
return nrows
Datasets:数据集:
first dataset:第一个数据集:
Second dataset:第二个数据集:
First add header to the second dataframe
with:首先将 header 添加到第二个
dataframe
中:
df2.columns = df1.columns
Or, much better, define them in the first place when reading the file with:或者,更好的是,在读取文件时首先定义它们:
df2 = pd.read_csv(data2, header=None, names=df1.columns.tolist())
And then inner merge
them to stay with just the rows that exists identically in both:然后
inner merge
它们以仅保留两者中相同存在的行:
united_df = df1.merge(df2, how='inner')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.