Python Pandas concat:ValueError:传递的值的形状是blah,索引意味着blah2

Python Pandas concat:ValueError:传递的值的形状是blah,索引意味着blah2,python,pandas,Python,Pandas,我正在尝试合并一个(熊猫14.1)数据帧和一个系列。序列应该形成一个新列,带有一些NAs(因为序列的索引值是数据帧索引值的子集) 这适用于玩具示例,但不适用于我的数据(详细信息如下) 例如: import pandas as pd import numpy as np df1 = pd.DataFrame(np.random.randn(6, 4), columns=['A', 'B', 'C', 'D'], index=pd.date_range('1/1/2011', periods=6,

我正在尝试合并一个(熊猫14.1)数据帧和一个系列。序列应该形成一个新列,带有一些NAs(因为序列的索引值是数据帧索引值的子集)

这适用于玩具示例,但不适用于我的数据(详细信息如下)

例如:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(6, 4), columns=['A', 'B', 'C', 'D'], index=pd.date_range('1/1/2011', periods=6, freq='D'))
df1

A   B   C   D
2011-01-01  -0.487926   0.439190    0.194810    0.333896
2011-01-02  1.708024    0.237587    -0.958100   1.418285
2011-01-03  -1.228805   1.266068    -1.755050   -1.476395
2011-01-04  -0.554705   1.342504    0.245934    0.955521
2011-01-05  -0.351260   -0.798270   0.820535    -0.597322
2011-01-06  0.132924    0.501027    -1.139487   1.107873

s1 = pd.Series(np.random.randn(3), name='foo', index=pd.date_range('1/1/2011', periods=3, freq='2D'))
s1

2011-01-01   -1.660578
2011-01-03   -0.209688
2011-01-05    0.546146
Freq: 2D, Name: foo, dtype: float64

pd.concat([df1, s1],axis=1)

A   B   C   D   foo
2011-01-01  -0.487926   0.439190    0.194810    0.333896    -1.660578
2011-01-02  1.708024    0.237587    -0.958100   1.418285    NaN
2011-01-03  -1.228805   1.266068    -1.755050   -1.476395   -0.209688
2011-01-04  -0.554705   1.342504    0.245934    0.955521    NaN
2011-01-05  -0.351260   -0.798270   0.820535    -0.597322   0.546146
2011-01-06  0.132924    0.501027    -1.139487   1.107873    NaN
数据的情况(见下文)似乎基本相同——包含一个带有DatetimeIndex的序列,其值是dataframe的子集。但是它在标题中给出了ValueError(blah1=(5286)blah2=(5276))。为什么不起作用

In[187]: df.head()
Out[188]:
high    low loc_h   loc_l
time                
2014-01-01 17:00:00 1.376235    1.375945    1.376235    1.375945
2014-01-01 17:01:00 1.376005    1.375775    NaN NaN
2014-01-01 17:02:00 1.375795    1.375445    NaN 1.375445
2014-01-01 17:03:00 1.375625    1.375515    NaN NaN
2014-01-01 17:04:00 1.375585    1.375585    NaN NaN
In [186]: df.index
Out[186]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01 17:00:00, ..., 2014-01-01 21:30:00]
Length: 271, Freq: None, Timezone: None

In [189]: hl.head()
Out[189]:
2014-01-01 17:00:00    1.376090
2014-01-01 17:02:00    1.375445
2014-01-01 17:05:00    1.376195
2014-01-01 17:10:00    1.375385
2014-01-01 17:12:00    1.376115
dtype: float64

In [187]:hl.index
Out[187]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01 17:00:00, ..., 2014-01-01 21:30:00]
Length: 89, Freq: None, Timezone: None

In: pd.concat([df, hl], axis=1)
Out: [stack trace] ValueError: Shape of passed values is (5, 286), indices imply (5, 276)
[187]中的
:df.head()
出[188]:
高-低位置h位置l
时间
2014-01-01 17:00:00 1.376235    1.375945    1.376235    1.375945
2014-01-01 17:01:00 1.376005 1.375775楠楠楠
2014-01-01 17:02:00 1.375795 1.375445南1.375445
2014-01-01 17:03:00 1.375625 1.375515楠楠楠
2014-01-01 17:04:00 1.375585 1.375585楠楠楠
In[186]:df.index
出[186]:
[2014-01-01 17:00:00, ..., 2014-01-01 21:30:00]
长度:271,频率:无,时区:无
[189]:hl.head()
出[189]:
2014-01-01 17:00:00    1.376090
2014-01-01 17:02:00    1.375445
2014-01-01 17:05:00    1.376195
2014-01-01 17:10:00    1.375385
2014-01-01 17:12:00    1.376115
数据类型:64
In[187]:hl.index
Out[187]:
[2014-01-01 17:00:00, ..., 2014-01-01 21:30:00]
长度:89,频率:无,时区:无
In:pd.concat([df,hl],轴=1)
Out:[堆栈跟踪]ValueError:传递值的形状是(5286),索引意味着(5276)

Aus_lacy的帖子给了我尝试相关方法的想法,其中,join确实有效:

In [196]:

hl.name = 'hl'
Out[196]:
'hl'
In [199]:

df.join(hl).head(4)
Out[199]:
high    low loc_h   loc_l   hl
2014-01-01 17:00:00 1.376235    1.375945    1.376235    1.375945    1.376090
2014-01-01 17:01:00 1.376005    1.375775    NaN NaN NaN
2014-01-01 17:02:00 1.375795    1.375445    NaN 1.375445    1.375445
2014-01-01 17:03:00 1.375625    1.375515    NaN NaN NaN
不过,了解一下为什么concat会在这个示例上工作,而不是这个数据会很好

我也有类似的问题(
join
有效,但
concat
失败)

检查
df1
s1
中是否存在重复的索引值(例如
df1.index.is_unique


删除重复的索引值(例如,
df.drop\u duplicates(inplace=True)
)或此处的某个方法应该可以解决此问题。

我的问题是不同的索引,以下代码解决了我的问题

df1.reset_index(drop=True, inplace=True)
df2.reset_index(drop=True, inplace=True)
df = pd.concat([df1, df2], axis=1)

您的索引可能包含重复的值

import pandas as pd

T1_INDEX = [
    0,
    1,  # <= !!! if I write e.g.: "0" here then it fails
    0.2,
]
T1_COLUMNS = [
    'A', 'B', 'C', 'D'
]
T1 = [
    [1.0, 1.1, 1.2, 1.3],
    [2.0, 2.1, 2.2, 2.3],
    [3.0, 3.1, 3.2, 3.3],
]

T2_INDEX = [
    1.2,
    2.11,
]

T2_COLUMNS = [
    'D', 'E', 'F',
]
T2 = [
    [54.0, 5324.1, 3234.2],
    [55.0, 14.5324, 2324.2],
    # [3.0, 3.1, 3.2],
]
df1 = pd.DataFrame(T1, columns=T1_COLUMNS, index=T1_INDEX)
df2 = pd.DataFrame(T2, columns=T2_COLUMNS, index=T2_INDEX)


print(pd.concat([pd.DataFrame({})] + [df2, df1], axis=1))
将熊猫作为pd导入
T1_指数=[
0,

1,#在连接它们之后尝试对索引进行排序

result=pd.concat([df1,df2]).sort_index()

要删除重复索引,请在4月18日15:25使用
df=df.loc[df.index.drop_duplicates()

这是错误的,但由于声誉低下,我无法直接回复BallpointBen的评论。错误的原因是
df.index.drop_duplicates()
返回唯一索引的列表,但当您使用这些唯一索引将索引返回到数据帧时,它仍会返回所有记录。我认为这可能是因为使用其中一个重复索引进行索引将返回该索引的所有实例

相反,使用
df.index.duplicated()
,它返回一个布尔列表(添加
~
以获取不重复的记录):


也许很简单,试试这个 如果您有一个数据框。那么请确保您尝试组合的两个矩阵或向量都具有相同的行\u名称/索引

我也有同样的问题。我更改了行的名称索引,使它们相互匹配 这是一个矩阵(主成分)和向量(目标)具有相同行标记的示例(我在图片左侧用蓝色圈出了它们)

在“当它不起作用”之前,我有一个正常行索引(0,1,2,3)的矩阵,而我有一个行索引(ID0,ID1,ID2,ID3)的向量 然后我将向量的行索引更改为(0,1,2,3),它对我有效


您是否尝试了
append
而不是
concat
?如果我正确理解
ValueError
的意思是有286行数据,但是数据帧的索引需要276行。请尝试签出
len(df.index)
len(h1.index)
.df.append(hl)TypeError失败:“NoneType”对象不可编辑。但我尝试了加入-谢谢!:)没问题。请确保将您的答案标记为正确,以便将来用户在遇到类似问题时可以快速找到您的解决方案。如果允许,我会这样做。错误消息可能会更有帮助,比如说“你可能有一些重复的索引”…谢谢!我是这样做的:df=pd.concat([df1,df2],axis=1,join_axes=[df1.index])。如果我在df2中有dup,那么我会得到这个错误。这很有意义,因为它不知道如何在两个DFs之间映射多个重复的索引。要删除重复的索引,请使用
df=df.loc][df.index.drop_duplicates()]
.C.f.检查两个索引中的重复索引值的建议可能有助于许多阅读此问题的人删除重复索引,最好是
df=df[~df.index.duplicated(keep='first')]
请看我最终遇到了这个问题并重置了_index()解决了。原始索引中的问题是什么?reset_index()是如何解决的?您能以代码形式显示它吗?
df = df.loc[~df.index.duplicated()]