I have been running into this problem a lot. If you have an existing DataFrame in Pandas, and you want to add a row that is simply an increasing count, ie. 0, 1, 2..., what is the most efficient way to do it?
Thanks!
Sam
sum() to Sum All Columns. Use DataFrame. sum() to get sum/total of a DataFrame for both rows and columns, to get the total sum of columns use axis=1 param. By default, this method takes axis=0 which means summing of rows.
To sum all the rows of a DataFrame, use the sum() function and set the axis value as 1. The value axis 1 will add the row values.
The easiest way might be
df = df.reset_index()
This will give you a new index starting at 0.
You could also do
df['counter'] = range(len(df))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With