Q:

Improving readability of pandas chains

I am used to writing one-liners for whatever pandas operations/changing I have, and it is a bit difficult for me to read/understand after I've come back to it (similar to writing one long SQL statement...). What are some ways to improve readability on chained operations? Right now I was trying something like:
res = (
    # (1) we filter on new__status_group = 'Unknown'
    df[df['new__status_group'] == 'UNKNOWN']

    # (2) we only care about these two files
    [['new__status', 'file_name']]

    # (3) group by the new status
    .groupby('new__status')

    # (4) we want to get the count and value of file_name
    .agg({'file_name': 'first', 'new__status': 'size'})

    # (5) rename the dummy column we used to grab the count
    .rename(columns={'new__status': 'count'})

    # (6) sort the values by count desc
    .sort_values('count', ascending=False)

    # (7) now that we're all good, reset the index so its like a normal data frame with all the fields
    .reset_index()

    # (8) limit to the top ten
    .head(10)

    # (9) and finally we want to pass it as a list of records (dict) for the end usage
    .to_dict('records')

)

Is this a good style? Or is this way too heavy-handed and verbose? What are some other ways to improve readability of the pandas functions?

A:

Two improvement, first use loc not chain slice , second agg can pass the name , so you do not need rename
res = (
    df.loc[df['new__status_group'] == 'UNKNOWN', ['new__status', 'file_name']]
    .groupby('new__status')
    .agg(file_name=('file_name','first'), count=('new__status', 'size'))
    .sort_values('count', ascending=False)
    .reset_index()
    .head(10)
    .to_dict('records')
)

