Reorder(Sort) rows in CSV using python based on other column

September 13, 2017 at 11:36:00
Specs: Windows, 6
I am trying to reorder rows in csv where I want to have "All" as the last row, in csv. I have tried using sort_index and sort_Values but I have not been able to achieve what I want. Here is my code:

import pandas as pd
import csv

df1=pd.read_csv("C:\\testfolder\\testdemofinal1.csv",sep=',',na_filter=False)

df=pd.DataFrame(df1)

a=df.pivot_table(index=['Parameter1_Calculation','Parameter2_Calculation'],columns='Measure Names', values='Measure Values',aggfunc='first',dropna=True,margins_name='All')
#a=a.sort_values(by='Total Earn',ascending=0)

This does sort the rows but does not give me the right result.

#a=a.sort_index(by=['Parameter1_Calculation','Parameter2_Calculation'],ascending=[True,False])
This gives me error.

b=a.to_csv("C:\\newfile1.csv",sep=',')


I have a sample of textual data below:

Parameter1 Parameter2 CPS CallE Calls ClickEarn Clicks
ABC - Health 8/23/2017 0.78 0 0 31.5 15
Ad Network 8/23/2017 0.01 0 0 1.3 1
All All 0.27 0 0 17,502,274 3,493,532
Quotes 8/23/2017 0 0 0 0 0


See More: Reorder(Sort) rows in CSV using python based on other column

Reply ↓  Report •
Related Solutions


Ask Question