I have twoDataFrame
df1=
iden c A1 A2 A3
11 1 1 1 NaN
23 2 3 3 NaN
33 3 2 2 1
25 4 NaN 1 NaN
74 1 NaN NaN NaN
df2=
iden
11
77
99
25
74
I need to create a new column in df1
such that if the values of iden
in df2
are in iden
of df1
then assign them the value 'Type 1', otherwise 'Type 2'.
The resulting DataFrame would be:
resultado=
iden c A1 A2 A3 Tipo
11 1 1 1 NaN Tipo 1
23 2 3 3 NaN Tipo 2
33 3 2 2 1 Tipo 2
25 4 NaN 1 NaN Tipo 1
74 1 NaN NaN NaN Tipo 1
At the moment I have the filter of those who are type 1:
filtro = df1[df1.identifica.isin(df2.identifica)]
I thought about using the apply
and the isin
but I don't know how to build the statement to relate the two DataFrame within the condition
df1['Tipo'] = df1['iden'].apply(lambda x: [....] )
I appreciate what you can help me
A very simple way is to generate a series of logics depending on whether
iden
dedf1
is indf2
, to verify this, we haveisin()
:Finally, based on these logics we establish the string to incorporate into the column