happycoder
happycoder

Reputation: 3

Get matching rows in two different dataframe python

Thanks in advance- the problem is to compare rows of two separate dataframes of csv files; with and without column headings. I want to match rows in second dataframe to rows in dataframe one. I cannot use merge because both don't have common column names to merge with.

1: The first dataframe have headings

2: Second dataframe is without headings.

3: get the position of the match

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:
first dataset

Second dataset:
Second dataset

Upvotes: 0

Views: 308

Answers (1)

Aryerez
Aryerez

Reputation: 3495

First add header to the second dataframe with:

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:

united_df = df1.merge(df2, how='inner')

Upvotes: 1

Related Questions