Python Forum
compound columns using IN - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: compound columns using IN (/thread-19910.html)



compound columns using IN - rregorr - Jul-19-2019

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"?


RE: compound columns using IN - scidam - Jul-20-2019

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...


RE: compound columns using IN - rregorr - Jul-22-2019

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.


RE: compound columns using IN - scidam - Jul-22-2019

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.