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
#1
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
Reply


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 1,150 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Using pyodbc&pandas to load a Table data to df tester_V 3 830 Sep-09-2023, 08:55 PM
Last Post: tester_V
  how do you style data frame that has empty rows. gsaray101 0 537 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,237 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  shutil.move make data corrupt kucingkembar 0 805 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Load multiple Jason data in one Data Frame vijays3 6 1,565 Aug-12-2022, 05:17 PM
Last Post: vijays3
  conditionals based on data frame mbrown009 1 905 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Merging two Data Frame on a special case piku9290dgp 0 1,093 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  Save data frame to .csv df.to.csv() mcva 1 1,547 Feb-03-2022, 07:05 PM
Last Post: mcva
  Iterating Through Data Frame Rows JoeDainton123 2 2,949 Aug-09-2021, 07:01 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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