[英]How to create a series of numbers using Pandas in Python
I am new to python and have recently learnt to create a series in python using Pandas.我是 python 的新手,最近学会了使用 Pandas 在 python 中创建一个系列。 I can define a series eg:
x = pd.Series([1, 2, 3, 4, 5])
but how to define the series for a range, say 1 to 100 rather than typing all elements from 1 to 100?我可以定义一个系列,例如:
x = pd.Series([1, 2, 3, 4, 5])
但是如何定义一个范围的系列,比如 1 到 100,而不是输入从 1 到 100 的所有元素?
As seen in the docs for pandas.Series , all that is required for your data
parameter is an array-like, dict, or scalar value .正如在pandas.Series 的文档中所见,您的
data
参数所需要的只是一个类似数组、字典或标量值。 Hence to create a series for a range, you can do exactly the same as you would to create a list for a range.因此,要为某个范围创建一个系列,您可以执行与为某个范围创建列表完全相同的操作。
one_to_hundred = pd.Series(range(1,101))
one_to_hundred=pd.Series(np.arange(1,101,1))
这是使用 numpy arange 函数创建系列的正确答案,该函数通过递增 1 创建从 1 到 100 的范围。
num=np.arange(1,101) num=np.arange(1,101)
s=pd.Series(num) s=pd.Series(num)
see the solution just change whatever you want.查看解决方案只需更改您想要的任何内容。 and for details about np.arange see below link https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.arange.html
有关 np.arange 的详细信息,请参阅以下链接https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.arange.html
There's also this:还有这个:
one_to_hundred = pd.RangeIndex(1, 101).to_series()
I'm still looking for a pandas function that creates a series containing a range (sequence) of numbers directly, but I don't think it exists.我仍在寻找一个 Pandas 函数,它可以直接创建一个包含数字范围(序列)的系列,但我认为它不存在。
try pd.Series([0 for i in range(20)])
.尝试
pd.Series([0 for i in range(20)])
。 It will create a pd series with 20 rows它将创建一个 20 行的 pd 系列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.