[英]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?我有一个数据框,我想添加一个值为 0、0.005、0.010、0.015 的列...直到 df 的长度,我该如何继续? thanks
谢谢
Only Pandas:仅 Pandas:
df = df.assign(newcol=pd.Series(range(df.shape[0])) * 0.005)
Using Numpy:使用 Numpy:
df = df.assign(newcol=np.arange(df.shape[0]) * 0.005)
One way to get there would be with the numpy.arange
function:到达那里的一种方法是使用
numpy.arange
function:
import numpy as np
df['new_column'] = np.arange(0, (df.shape[0]*0.005), 0.005)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.