Unable to join on multiple columns with Pandas

Advertisements

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 …

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

Leave a ReplyCancel reply