无法使用Pandas在多个列上进行连接。

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月10日 20:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定