Al_Mt
Al_Mt

Reputation: 167

How to get rows from one dataframe based on another dataframe

I just edited the question as maybe I didn't make myself clear.

I have two dataframes (MR and DT)

The column 'A' in dataframe DT is a subset of the column 'A' in dataframe MR, they both are just similar (not equal) in this ID column, the rest of the columns are different as well as the number of rows.

How can I get the rows from dataframe MR['ID'] that are equal to the dataframe DT['ID']? Knowing that values in 'ID' can appear several times in the same column.

The DT is 1538 rows and MR is 2060 rows).

I tried some lines proposed here >https://stackoverflow.com/questions/28901683/pandas-get-rows-which-are-not-in-other-dataframe but I got bizarre results as I don't fully understand the methods they proposed (and the goal is little different)

Thanks!

Upvotes: 0

Views: 3339

Answers (1)

NotAName
NotAName

Reputation: 4322

Take a look at pandas.Series.isin() method. In your case you'd want to use something like:

matching_id = MR.ID.isin(DT.ID)  # This returns a boolean Series of whether values match or not

# Now filter your dataframe to keep only matching rows
new_df = MR.loc[matching_id, :]

Or if you want to just get a new dataframe of combined records for the same ID you need to use merge():

new_df = pd.merge(MR, DT, on='ID')

This will create a new dataframe with columns from both original dfs but only where ID is the same.

Upvotes: 1

Related Questions