英文:
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’
我尝试了三种不同的方法 Dynamic = ['Col1','Col2'] df = df[['|'.join(Dynamic),'Col3']] 结果 KeyError: "[‘Col1|Col2’] not in index" df = df[[','.join(Dynamic),'Col3']] 结果 KeyError: "[‘Col1,Col2’] not in index" df = df[","’.join(Dynamic),'Col3']] 结果 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)
这是我的查询目前的样子...
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
基本查询(示例1)没有问题
Example 1 df = df['Col1','Col2','Col3] - Works
示例1 df = df['Col1','Col2','Col3] - 可行
as well as Example 2. Here I use the .join Method.
以及示例2。这里我使用.join方法。
Example 2 Dynamic = ['Col2'] df = df[['Col1','|'.join(Dynamic),'Col3']]
示例2 Dynamic = ['Col2'] df = df[['Col1','|'.join(Dynamic),'Col3']]
Any idea how to resolve this?
有什么解决方法吗?
Thanks for your help.
感谢您的帮助。
英文:
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.
答案1
得分: 4
你正在尝试的操作需要使用列表索引,而不是字符串索引。所以只需使用列表连接:
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']]
输出:
Col1 Col2 Col3
0 first_value first_value first_value
1 second_value second_value second_value
或者
Dynamic = ['Col2']
df[['Col1'] + Dynamic + ['Col3']]
输出:
Col1 Col2 Col3
0 first_value first_value first_value
1 second_value second_value second_value
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论