Pandas dataframe to excel: AttributeError: 'list' object has no attribute 'to_excel'

11,840

This comment from @roganjosh did the trick:

No, it's the [ and ] on either end of that line. That makes it a single-element list. df1.groupby('ClientID')['BeginDate'].last() will give you the dataframe

Once I took away the brackets, everything worked as intended.

Share:
11,840
clarktwain
Author by

clarktwain

Updated on December 06, 2022

Comments

  • clarktwain
    clarktwain over 1 year

    I'm working with a pandas dataframe, where I take an excel file, group for the maximum date in one column by the client ID in another. I want to save that as an excel file, so that I can check my work and make sure my output is what I want.

    My code is as follows:

    df1 = pd.read_excel('ClientTrackExport.xlsx') 
    grouped = [df1.groupby('ClientID')['BeginDate'].last()]
    writer = pd.ExcelWriter('examples.xlsx') 
    grouped.to_excel(writer, 'Sheet 1', index=False) 
    writer.save()
    

    And I get this error message:

    AttributeError: 'list' object has no attribute 'to_excel'
    

    I followed the Pandas documentation to the letter, so I'm stuck. Any thoughts?