Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python pandas select columns combining list and other column name

I have created a list with 2 column names of a data frame.

Now I want to use this list to select this 2 columns along with other columns.

I have for example a data frame with 5 columns: A, B, C, D, E

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I create a list that references A and B:

lst = ['A','B']

Then when I try to print it adding C I get an error.

print(df[lst,'C'])

TypeError: ‘([‘A’, ‘B’], ‘C’)’ is an invalid key

Which is the correct way to select the columns in a list along with others?

thanks!

>Solution :

Need to pass that as list too;

df[lst+['C']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading