sunny
sunny

Reputation: 355

pandas compare two column with different size

Suppose we have two columns in two dataframes , the columns are the same but the size is different. How do we compare two columns and have the indices of the same values in both? df1 and df2, age is common in two but the df1 has 1000 rows and the df2 has 200 rows -- I want to have the indices of rows that have the same age value?

Upvotes: 3

Views: 4010

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

You can use .loc for index labelling:

df1.age < df2.loc[df1.index].age

Example:

df1 = pd.DataFrame({'age':np.random.randint(1,10,10)})

df2 = pd.DataFrame({'age':np.random.randint(1,10,20)})

Output:

0     True
1     True
2    False
3     True
4     True
5    False
6    False
7     True
8    False
9    False
Name: age, dtype: bool

Get everything all in one dataframe:

df1.assign(age_2=df2.loc[df1.index],cond=df1.age < df2.loc[df1.index].age)

Output:

   age  age_2   cond
0    3      5   True
1    3      8   True
2    6      6  False
3    4      7   True
4    4      7   True
5    5      2  False
6    2      2  False
7    3      7   True
8    6      3  False
9    5      4  False

Upvotes: 2

Related Questions