How to sort Excel sheet using Python

后端 未结 2 1245
星月不相逢
星月不相逢 2021-01-06 13:44

I am using Python 3.4 and xlrd. I want to sort the Excel sheet based on the primary column before processing it. Is there any library to perform this ?

2条回答
  •  生来不讨喜
    2021-01-06 14:27

    I just wanted to refresh the answer as the Pandas implementation has changed a bit over time. Here's the code that should work now (pandas 1.1.2).

    import pandas as pd
    
    xl = pd.ExcelFile("test.xlsx")
    df = xl.parse("Sheet1")
    df = df.sort_values(by="Header Row")
    ...
    

    The sort function is now called sort_by and columns is replaced by by.

提交回复
热议问题