Pandas groupby to to_csv

前端 未结 5 546
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 03:42

Want to output a Pandas groupby dataframe to CSV. Tried various StackOverflow solutions but they have not worked.

Python 3.6.1, Pandas 0.20.1

groupby result

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 03:55

    Try changing your second line to week_grouped = week_grouped.sum() and re-running all three lines.

    If you run week_grouped.sum() in its own Jupyter notebook cell, you'll see how the statement returns the output to the cell's output, instead of assigning the result back to week_grouped. Some pandas methods have an inplace=True argument (e.g., df.sort_values(by=col_name, inplace=True)), but sum does not.

    EDIT: does each week number only appear once in your CSV? If so, here's a simpler solution that doesn't use groupby:

    df = pd.read_csv('input.csv')
    df[['id', 'count']].to_csv('output.csv')
    

提交回复
热议问题