【问题标题】:Concat two dataframes: Reindexing only valid with uniquely valued Index objects连接两个数据框:重新索引仅对具有唯一值的索引对象有效
【发布时间】:2022-01-02 23:48:03
【问题描述】:

我想垂直连接 2 个数据帧,但出现此错误:

重新索引仅对具有唯一值的索引对象有效

我该如何解决?

df1

      TimeStamp  Input     X     Y  Time  Distance  Speed Pressure Tilt_X Tilt_X
  16    79769.0  aaaaa  8898  8438   NaN       NaN    NaN     None   None   None
  17    79784.0  aaaaa  8898  8438  15.0       0.0    0.0     None   None   None
  18    79793.0  aaaaa  8898  8438   9.0       0.0    0.0     None   None   None
  19    79802.0  aaaaa  8898  8438   9.0       0.0    0.0     None   None   None

df2

      TimeStamp Input     X     Y  Time  Distance  Speed Pressure Tilt_X Tilt_Y
  26    84456.0   bbb  8762  9318   NaN       NaN    NaN        0      0      0
  27    84459.0   bbb  8762  9318   3.0       0.0    0.0     4069  -1397  -1445
  28    84459.0   bbb  8762  9318   0.0       0.0    0.0     4069  -1397  -1445
  29    84464.0   bbb  8762  9318   5.0       0.0    0.0     3944  -1397  -1445
  30    84472.0   bbb  8762  9318   8.0       0.0    0.0     3692  -1397  -1445
  31    84482.0   bbb  8741  9253  10.0       0.6    0.0     3317  -1397  -1445

我想连接到这个:

      TimeStamp  Input     X     Y  Time  Distance  Speed Pressure Tilt_X Tilt_X
  16    79769.0  aaaaa  8898  8438   NaN       NaN    NaN     None   None   None
  17    79784.0  aaaaa  8898  8438  15.0       0.0    0.0     None   None   None
  18    79793.0  aaaaa  8898  8438   9.0       0.0    0.0     None   None   None
  19    79802.0  aaaaa  8898  8438   9.0       0.0    0.0     None   None   None
  26    84456.0    bbb  8762  9318   NaN       NaN    NaN        0      0      0
  27    84459.0    bbb  8762  9318   3.0       0.0    0.0     4069  -1397  -1445
  28    84459.0    bbb  8762  9318   0.0       0.0    0.0     4069  -1397  -1445
  29    84464.0    bbb  8762  9318   5.0       0.0    0.0     3944  -1397  -1445
  30    84472.0    bbb  8762  9318   8.0       0.0    0.0     3692  -1397  -1445
  31    84482.0    bbb  8741  9253  10.0       0.6    0.0     3317  -1397  -1445

但是下面的代码导致Reindexing only valid with uniquely valued Index objects

aaaaa_tmp_df = pd.concat([aaaaah_Time_df,
                          aaaaa_input_df,
                          aaaaa_X_df,
                          aaaaa_Y_df,
                          aaaaa_time_df_diff,
                          aaaaa_xy_real_dis_df,
                          aaaaa_speed_df,
                          aaaaa_Pressure_df,
                          aaaaa_TiltX_df,
                          aaaaa_TiltY_df],axis = 1)    
aaaaa_tmp_df.columns=['TimeStamp',
                      'Input',
                      'X',
                      'Y',
                      'Time',
                      'Distance',
                      'Speed',
                      'Pressure',
                      'Tilt_X',
                      'Tilt_X']

bbb_tmp_df = pd.concat([bbb_Time_df,
                        bbb_input_df,
                        bbb_X_df,
                        bbb_Y_df,
                        bbb_time_df_diff,
                        bbb_xy_real_dis_df,
                        bbb_speed_df,
                        bbb_Pressure_df,
                        bbb_TiltX_df,
                        bbb_TiltY_df],axis = 1)    
bbb_tmp_df.columns=['TimeStamp',
                    'Input',
                    'X',
                    'Y',
                    'Time',
                    'Distance',
                    'Speed',
                    'Pressure',
                    'Tilt_X',
                    'Tilt_Y']


aaaaa = aaaaa_tmp_df[aaaaa_tmp_df['Input'] == 'aaaaa']
bbb = bbb_tmp_df[bbb_tmp_df['Input'] == 'bbb']

all_df = pd.concat([aaaaa,bbb],axis = 0, ignore_index=True)

错误信息:

Traceback (most recent call last):

File "D:\Python\Digiinfo_Parser\Digiinfo_Parser.py", line 276, in 
  <module>
all_df = pd.concat([touch,pen,ptp],axis = 0, ignore_index=True)

File "C:\Users\ANDYCHEN\Anaconda3\lib\site- 
packages\pandas\core\reshape\concat.py", line 298, in concat
return op.get_result()

File "C:\Users\ANDYCHEN\Anaconda3\lib\site- 
packages\pandas\core\reshape\concat.py", line 516, in get_result
indexers[ax] = obj_labels.get_indexer(new_labels)

File "C:\Users\ANDYCHEN\Anaconda3\lib\site- 
packages\pandas\core\indexes\base.py", line 3171, in get_indexer
raise InvalidIndexError(

InvalidIndexError: Reindexing only valid with uniquely valued Index 
objects

【问题讨论】:

  • a concat 为我工作没有问题,你能仔细检查你的例子吗?另请提供您使用的确切命令
  • 代码已附加

标签: python pandas dataframe


【解决方案1】:

您想垂直使用concat,但使用了axis=1。去掉这个参数:

pd.concat([input_df, time_df, contactid_df, x_df, y_df, pressure_df, tiltx_df, tilty_df])

【讨论】:

  • 我想连接all_df = pd.concat([touch,pen,ptp],axis = 0, ignore_index=True),得到同样的错误
  • 请提供完整的错误和一个最小可重现的例子
【解决方案2】:

这里的InvalidIndexError实际上是指索引。

df1 有重复的列名:

... Pressure Tilt_X Tilt_X

pd.concat 不适用于重复的列名。

在这种情况下,看起来第二个 Tilt_X 实际上应该是 Tilt_Y,但您应该检查所有数据框的列以确保没有其他重复项。

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 2022-07-15
    • 2021-07-22
    • 2021-05-22
    • 2018-10-27
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多