CHL
CHL

Reputation: 57

Concat two dataframes: Reindexing only valid with uniquely valued Index objects

I want to concat 2 dataframes vertically, but I get this error:

Reindexing only valid with uniquely valued Index objects

How can I fix it?

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

I want to concat to this:

      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

But the following code results in 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)

Error msg:

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

Upvotes: 1

Views: 1711

Answers (2)

tdy
tdy

Reputation: 41327

Here the InvalidIndexError is actually referring to the column index.

df1 has duplicate column names:

... Pressure Tilt_X Tilt_X

pd.concat does not work with duplicate column names.

In this case it looks like the second Tilt_X should actually be Tilt_Y, but you should check all of your dataframes' columns to make sure there are no other duplicates.

Upvotes: 1

mozway
mozway

Reputation: 260480

You want to concat vertically, but used axis=1. Remove this parameter:

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

Upvotes: 0

Related Questions