Python Forum
Move a particular row in pandas data frame to last row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Move a particular row in pandas data frame to last row
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'],
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;

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\", line 790, in pop result = self[item] File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\", line 2787, in __getitem__ return self.where(key) File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\", line 8918, in where return self._where( File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\", 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;

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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 856 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Using pyodbc&pandas to load a Table data to df tester_V 3 698 Sep-09-2023, 08:55 PM
Last Post: tester_V
  how do you style data frame that has empty rows. gsaray101 0 467 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,076 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  shutil.move make data corrupt kucingkembar 0 693 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Load multiple Jason data in one Data Frame vijays3 6 1,444 Aug-12-2022, 05:17 PM
Last Post: vijays3
  conditionals based on data frame mbrown009 1 851 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Merging two Data Frame on a special case piku9290dgp 0 1,042 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  Save data frame to .csv mcva 1 1,441 Feb-03-2022, 07:05 PM
Last Post: mcva
  Iterating Through Data Frame Rows JoeDainton123 2 2,796 Aug-09-2021, 07:01 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020