Python Forum

Full Version: Move a particular row in pandas data frame to last row
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to move a particular row in the below data frame to the last position of the data frame.
Can someone help me to achieve this?

import pandas as pd
df1 = pd.DataFrame(data = {'name':["Total",'Tozi Ford','Susan Mock','Donale Fucci'],
                               'store_label':["np.nan",'Merchant_A','Merchant_B','Merchant_C'],
                               "earned_amount":[4300,1000,300,3000],
                                })
I want to shift the row having "Total" in the name column to the last row.

row_to_be_moved =df1.iloc[df1.index[df1['name'] == "Total"],:]
df1.insert([-1], df1.pop(row_to_be_moved))
I'm getting the following error;

Error:
df1.insert([-1], df1.pop(row_to_be_moved)) Traceback (most recent call last): File "<ipython-input-272-4e6927fc2da0>", line 1, in <module> df1.insert([-1], df1.pop(row_to_be_moved)) File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 790, in pop result = self[item] File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2787, in __getitem__ return self.where(key) File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 8918, in where return self._where( File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 8677, in _where raise ValueError(msg.format(dtype=dt)) ValueError: Boolean array expected for the condition, not object
The resultant table I'm expecting is;

Output:
df1 Out[274]: name store_label earned_amount 0 Tozi Ford Merchant_A 1000 1 Susan Mock Merchant_B 300 2 Donale Fucci Merchant_C 3000 3 Total np.nan 4300