Home>

I would like to organize the contents of csv data using pandas of python.

start, id, time, eldid, Count_, keniki, x, y, botime, firev, detime, extime
A, 57136,0.223288997,1235,13391,1, -10171.5449606, -44468.0849596,2.8697357095172613,60.0,3.36993,0
B, 57136,0.29927726600000004,1235,13391,1, -10171.5449606, -44468.0849596,2.8697357095172613,60.0,3.36993,0
C, 57136,0.183266565,1235,13391,1, -10171.5449606, -44468.0849596,2.8697357095172613,60.0,3.36993,0

Specifically, when the following script is turned using the above csv file (part), the column "start" of the output csv data (part) will be changed from ascending to descending order. How do I change the script to make the "start" column order ascending?
Thank you.

for i in range (1,1001):
    df = pd.read_csv (f "list {i} _strategy1.csv")
    new_file = f "list {i} _strategy1.csv"
    df2 = df.drop (df.columns [[4, 8]], axis = 1)
    df2 = (df.ix [:, ["start", "id", "detime", "time"]])
    df3 = df2.sort_values ​​("detime")
    df3.to_csv (new_file, index = False)

Part of the output csv data
start, id, detime, time
C, 89101,0.401035,0.1688917
B, 89101,0.401035,0.279614717
A, 89101,0.401035,0.206245942

  • Answer # 1

    I don't think the understanding of "change from ascending to descending order" is accurate. There is no such thing as "changing in descending order".

    The default sorting algorithm for the

    DataFrame.sort_values ​​method is quick sort. Since quick sort is an unstable sort, the order of equivalent data is not kept as before. It is possible that the original order is not maintained as a result.

    pandas.DataFrame.sort_values ​​— pandas 0.25.3 documentation

    If you want to "order in the original data", all you need to do is specify thekindargument of thesort_values ​​method accordingly (" that's fine because it accepts mergesort "). Also, if you want to sort the start column regardless of the order of the original data, thebyargument of thesort_values ​​method islist of strCan also be used to specify["detime", "start"].