Python Forum
compound columns using IN
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
compound columns using IN
#1
I have a dataframe with 4 columns identifieds with names single and compounded "Identificação Única", "Nome", "Código da Rubrica" and "Valor" and I would like to get some values from these columns. e.i. numbers 10 and 11 from "Identificação Única" or numbers 1 and 2 from "Origem". The code following just works if the name of the columns is single, but it doesn't works if it is compound.

For example:

#f0619.query('Origem in ["1", "2"]') # it works fine
f0619.query('Código Rubrica in ["27","69"]') # it doesn't works, but if I rename these column to, let's say, Codigo_Rubrica, it works fine.
How can I to work with compound columns using "IN"?
Reply
#2
Are you allowed to use something else instead of .query. You can try, e.g.

df.iloc[:, 27].str.contains("Rubrica") & df.iloc[:, 69].str.contains("Rubrica")
Or may be I misunderstood something...
Reply
#3
I think that you didn't understand. I can not to use "loc". I prefer to use query this way:
f0619.query('CódigoRubrica in ["27","69"]')  #this way, it works.
But, when the column has two names, two string separated by spaces, I got error. For example:
f0619.query('Código Rubrica in ["27","69"]')
And I don't want rename the columns, and I don't want remove the empty spaces of words, because it is confuse.
I have to use the column the way it is.
Reply
#4
It seems that functionality is supported by the latest version of Pandas only.
Unfortunately, I don't have Pandas 0.25+ installed on my computer, so try the following suggestion.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020