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

Unable to join on multiple columns with Pandas

I am currently stuck with the following.

I want to create a set query, by making it more dynamic by just changing the column names. With one column it works, but it doesn’t with 2 or more.

I tried it in three different ways

Dynamic = ['Col1','Col2']

df = df[['|'.join(Dynamic),'Col3']] result KeyError: "['Col1|Col2'] not in index" 

df = df[[','.join(Dynamic),'Col3']] result KeyError: "['Col1,Col2'] not in index"

df = df[["','".join(Dynamic),'Col3']] result KeyError: '["Col1\',\'Col2"] not in index'

This is how my queries are looking right now …

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

import pandas as pd

df = {'Col1': ['first_value', 'second_value'],
      'Col2': ['first_value', 'second_value'] ,
      'Col3': ['first_value', 'second_value'] }

df = pd.DataFrame(df)
print(df)

The basic query (Example 1) works without an issue

Example 1
df = df['Col1','Col2','Col3] - Works

as well as Example 2. Here I use the .join Method.

Example 2
Dynamic = ['Col2']
df = df[['Col1','|'.join(Dynamic),'Col3']]

Any idea how to resolve this?

Thanks for your help.

>Solution :

What you are trying to do requires a list index, not a string one. So just use list concatenation:

df = {'Col1': ['first_value', 'second_value'],
      'Col2': ['first_value', 'second_value'] ,
      'Col3': ['first_value', 'second_value'] }

df = pd.DataFrame(df)
Dynamic = ['Col1','Col2']
df[Dynamic + ['Col3']]

Output:

           Col1          Col2          Col3
0   first_value   first_value   first_value
1  second_value  second_value  second_value

Or

Dynamic = ['Col2']
df[['Col1'] + Dynamic + ['Col3']]

Output:

           Col1          Col2          Col3
0   first_value   first_value   first_value
1  second_value  second_value  second_value
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