如何在 Python 中使用 Pandas 创建一系列数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36667548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a series of numbers using Pandas in Python
提问by MrCurious
I am new to python and have recently learnt to create a series in python using Pandas. 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?
我是 python 的新手,最近学会了使用 Pandas 在 python 中创建一个系列。我可以定义一个系列,例如: x = pd.Series([1, 2, 3, 4, 5])
但是如何定义一个范围的系列,比如 1 到 100,而不是输入从 1 到 100 的所有元素?
回答by miradulo
As seen in the docs for pandas.Series, all that is required for your data
parameter is an array-like, dict, or scalar value. Hence to create a series for a range, you can do exactly the same as you would to create a list for a range.
正如在pandas.Series 的文档中所见,您的data
参数所需要的只是一个类数组、字典或标量值。因此,要为某个范围创建一个系列,您可以执行与为某个范围创建列表完全相同的操作。
one_to_hundred = pd.Series(range(1,101))
回答by Sowmya Pai
one_to_hundred=pd.Series(np.arange(1,101,1))
This is the correct answer where you create a series using the numpy arange function which creates a range starting with 1 till 100 by incrementing 1.
这是使用 numpy arange 函数创建系列的正确答案,该函数通过递增 1 创建从 1 到 100 的范围。
回答by Shaonsani
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
回答by Matthias Fripp
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 函数,它可以直接创建一个包含数字范围(序列)的系列,但我认为它不存在。