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
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')