How to compare two string values with pandas?

Asked

Viewed 97 times

3

Hello!

I have two pandas dataframes and selected a column of each with ids. I want to compare the ids to see if the id of one dataframe is contained in another, but it does not have the same amount of lines and is not in the same order.

I tried to compare it this way

#Read data
import pandas as pd

cursos = pd.read_excel("planilha1.xlsx")
unidade_ensino = pd.read_excel("planilha2.xlsx", na_values=str)   
    
cursos = cursos.loc(cursos['codigo_unidade_ensino'] == unidade_ensino['cod_unidade_ensino'])

However, I received the following error:

Valueerror: Can only compare identically-labeled Series Objects

1 answer

2

Try using the function isin, in this way:

mask = cursos['codigo_unidade_ensino'].isin(unidade_ensino['cod_unidade_ensino'])
cursos = cursos[mask].copy()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.