Python 为什么 Pandas 内连接会给出 ValueError: len(left_on) 必须等于“right”索引中的级别数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28228781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:58:02  来源:igfitidea点击:

Why does Pandas inner join give ValueError: len(left_on) must equal the number of levels in the index of "right"?

pythonpandasjoinmergeinner-join

提问by Ian Joyce

I'm trying to inner join DataFrame A to DataFrame B and am running into an error.

我正在尝试将 DataFrame A 内部连接到 DataFrame B 并且遇到错误。

Here's my join statement:

这是我的加入声明:

merged = DataFrameA.join(DataFrameB, on=['Code','Date'])

And here's the error:

这是错误:

ValueError: len(left_on) must equal the number of levels in the index of "right"

I'm not sure the column order matters (they aren't truly "ordered" are they?), but just in case, the DataFrames are organized like this:

我不确定列顺序是否重要(它们不是真正“有序”的吗?),但以防万一,DataFrame 的组织方式如下:

DataFrameA:  Code, Date, ColA, ColB, ColC, ..., ColG, ColH (shape: 80514, 8 - no index)
DataFrameB:  Date, Code, Col1, Col2, Col3, ..., Col15, Col16 (shape: 859, 16 - no index)

Do I need to correct my join statement? Or is there another, better way to get the intersection (or inner join) of these two DataFrames?

我需要更正我的加入声明吗?或者是否有另一种更好的方法来获得这两个 DataFrame 的交集(或内部连接)?

采纳答案by JAB

use mergeif you are not joining on the index:

使用merge,如果你没有在指数加盟:

merged = pd.merge(DataFrameA,DataFrameB, on=['Code','Date'])

Follow up to question below:

跟进以下问题:

Here is a reproducible example:

这是一个可重现的示例:

import pandas as pd
# create some timestamps for date column
i = pd.to_datetime(pd.date_range('20140601',periods=2))

#create two dataframes to merge
df = pd.DataFrame({'code': ['ABC','EFG'], 'date':i,'col1': [10,100]})
df2 = pd.DataFrame({'code': ['ABC','EFG'], 'date':i,'col2': [10,200]})

#merge on columns (default join is inner)
pd.merge(df, df2, on =['code','date'])

This results is:

这个结果是:

    code    col1    date    col2
0   ABC     10      2014-06-01  10
1   EFG     100     2014-06-02  200

What happens when you run this code?

运行此代码时会发生什么?

回答by Jeru Luke

Here is another way of performing join. Unlike the answer verified, this is a more general answer applicable to all other types of join.

这是另一种表演方式join。与已验证的答案不同,这是适用于所有其他类型的 join的更通用的答案。

Inner Join

内部联接

inner joincan also be performed by explicitly mentioning it as follows in how:

inner join也可以通过在以下内容中明确提及它来执行how

pd.merge(df1, df2, on='filename', how='inner')

The same methodology aplies for the other types of join:

相同的方法适用于其他类型的连接:

OuterJoin

外连接

pd.merge(df1, df2, on='filename', how='outer')

Left Join

左加入

pd.merge(df1, df2, on='filename', how='left')

Right Join

右加入

pd.merge(df1, df2, on='filename', how='right')