Obtaining column values for a dataframe from another dataframe based on a common column variable
Just use pandas.merge to combine the two dataframes:
import pandas as pd

df1 = pd.DataFrame({&quot;animal&quot;: [&quot;Cat&quot;, &quot;Dog&quot;, &quot;Rat&quot;, &quot;Bull&quot;, &quot;Dog&quot;, &quot;Bull&quot;, &quot;Bull&quot;, &quot;Dog&quot;, &quot;Cat&quot;, &quot;Rat&quot;, &quot;Dog&quot;], &quot;lifeSpan&quot;: [2, 4, 6, 0, 4, 0, 0, 4, 2, 6, 4]})
df2 = pd.DataFrame({&quot;animal&quot;:[&quot;Bull&quot;, &quot;Cat&quot;, &quot;Rat&quot;, &quot;Dog&quot;]})

df3 = df2.merge(df1.drop_duplicates(), on=&quot;animal&quot;)

This returns the following dataframe:




animal
lifeSpan




Bull
0


Cat
2


Rat
6


Dog
4