简体   繁体   中英

Compare two columns and return the row numbers in Python

I have two columns A = [11, 24, 7, 7, 0, 4, 9, 20, 3, 5] and B = [5, 8, 9, 1, 11]. But they have different row numbers. I want to find if A has the same element as B does and return the row numbers of A and B. For example,

A and B have the same value 5,9, 11 and the returned matrix is C = [5, 9, 11]

The returned row number of A should be row_A = [9, 6, 0]

The returned row number of B should be row_B = [0, 2, 4]

Are there any function in python I can use? Thanks

This is numpy.intersect1d . Using the return_indices argument you can also get the indices in addition to the values. This will work if A/B are numpy arrays, pandas Series, or lists.

Note, if your objects are pandas objects, the returned indices are the array indices. So if your Series don't have a RangeIndex starting from 0, you can slice their Index objects by the array locations to get the actual Series Index labels.

import numpy as np

vals, idxA, idxB = np.intersect1d(A, B, return_indices=True)

vals
#array([ 5,  9, 11])

idxA
#array([9, 6, 0])

idxB
#array([0, 2, 4])

If you have two different dataframes, copy index in another column for each and join on columns:

df_A["index"] = df.index
df_B["index"] = df.index

df_A.merge(df_B, left_on="A", right_on="B", how="inner")


在此处输入图片说明

df = pd.DataFrame(data = {"A":[11, 24, 7, 7, 0, 4, 9, 20, 3, 5], "B" : [5, 8, 9, 1, 11, -1,-1,-1,-1,-1]})
df_A = df[["A"]]
df_A["index"] = df.index
df_B = df[["B"]]
df_B["index"] = df.index

df_A.merge(df_B, left_on="A", right_on="B", how="inner")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM