4
Hello, I need to relate the two dataframes below using conditions (i) if df1 cat is in the tuple of the df2 cat column; and (ii) the event column. The result should be a column 'Resp' in df1 that contains the respective data in df2.
Below I leave the examples of dataframes that resemble the original:
import pandas as pd
df1 = pd.DataFrame({
'Nome': ['João', 'Maria', 'Antonio', 'Pedro', 'João', 'Maria', 'Antonio', 'Pedro'],
'cat': ['A', 'A', 'B', 'C', 'A', 'A', 'B', 'C'],
'evento': [1, 1, 1, 1, 2, 2, 2, 2],
'sim/não': ['sim', 'não', 'sim', 'sim', 'não', 'sim', 'não', 'sim']
})
df2 = pd.DataFrame({
'cat': [('A', 'B'), ('C'), ('A', 'B'), ('C')],
'evento': [1, 1, 2, 2],
'resp': ['sim', 'não', 'não', 'não']
})
The final dataframe should look like this:
df_resultado = pd.DataFrame({
'Nome': ['João', 'Maria', 'Antonio', 'Pedro', 'João', 'Maria', 'Antonio', 'Pedro'],
'cat': ['A', 'A', 'B', 'C', 'A', 'A', 'B', 'C'],
'evento': [1, 1, 1, 1, 2, 2, 2, 2],
'sim/não': ['sim', 'não', 'sim', 'sim', 'não', 'sim', 'não', 'sim'],
'resp': ['sim', 'sim', 'sim', 'não', 'não', 'não', 'não', 'não']
})
I thought to use the "merge" method as follows:
df_resultado = df1.merge(df2, how='left', on=['evento', 'cat'])
However, with this logic the column 'Resp' in df1 has null values for the 'cat' A and B, since the method analyzed the tuples that are in df2.
Would you kindly indicate to me the syntax to make this relationship right?
It worked perfectly. Thank you!
– Acácio Telechi
If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.
– Lucas