anomellie
anomellie

Reputation: 1

add a sequence of numbers to a column in pandas data frame in Python

I have a data frame and I want to add a column with values 0, 0.005, 0.010, 0.015...up to the length of the df, how do I proceed? thanks

Upvotes: 0

Views: 871

Answers (2)

acrobat
acrobat

Reputation: 917

One way to get there would be with the numpy.arange function:

import numpy as np

df['new_column'] = np.arange(0, (df.shape[0]*0.005), 0.005)

Upvotes: 0

Pierre D
Pierre D

Reputation: 26211

Only Pandas:

df = df.assign(newcol=pd.Series(range(df.shape[0])) * 0.005)

Using Numpy:

df = df.assign(newcol=np.arange(df.shape[0]) * 0.005)

Upvotes: 1

Related Questions