0

I have a main DF and then a sub-DF that I want to insert within the main DF, between two rows. So the index of the first DF is ..., 183, 184, 185, 186, 187... I want to insert a five-row DF in between rows 185 and 186 in the main DF.

To do this, I am trying to set the index of the sub-DF to numbers between 185 and 186, and then reindex, but I am getting a key error on the first row.

The index list is [185.01, 185.02, 185.03, 185.04, 185.05]. The error is "KeyError: 185.01"

I feel like this should be possible based on this thread: Is it possible to insert a row at an arbitrary position in a dataframe using pandas?

        # reset then set index for inserting these rows after the rest of the items for this subtest
        row_index.clear()
        row_index = [round((last_subtest_item_index + 0.01) + (x * 0.01),2) for x in range(0, var_counter)]
        print(last_subtest_item_index, row_index)
        print(new_custom_table)

        # TRY TO INSERT THE CUSTOM TABLE DF INTO THE MAIN DF AND SEE IF IT GOES IN-LINE WITH THE REST OF THAT CRF
        new_custom_table.set_index(keys = row_index, inplace = True)
        self.full_CRF = self.full_CRF.append(new_custom_table, ignore_index = False).sort_index().reset_index(drop=True)
Korzak
  • 365
  • 7
  • 19

1 Answers1

0

The problem is that DataFrame.reset_index(keys=keys) requires keys to be of type Series, Index, or numpy.ndarray (link to docs), but you're giving it a python list, row_index. The fix is to wrap the list in a numpy array constructor.

Replace this line:

new_custom_table.set_index(keys = row_index, inplace = True)

with this:

new_custom_table.set_index(keys=pd.np.array(row_index), inplace=True)
Peter Leimbigler
  • 10,775
  • 1
  • 23
  • 37