'Pandas groupby feature question for output CSV

I have the following code

df.groupby('AccountNumber')[['TotalStake','TotalPayout']].sum()

which displays as I would like it to in pandas

The issue is when I output this to a CSV the first column AccountNumber disappears. How can I keep the AccountNumber column after using df.to_csv?

df = df.groupby('AccountNumber')[['TotalStake','TotalPayout']].sum()
df.to_csv("AccountNumbers.csv",index=False)



Solution 1:[1]

You could also do:

df = df.groupby('AccountNumber')[['TotalStake','TotalPayout']].sum().reset_index()
df.to_csv("AccountNumbers.csv", index=False)

Solution 2:[2]

df.to_csv("AccountNumbers.csv", index=True)

was the answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BeRT2me
Solution 2 Tyler2P