33

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?

Alexander
  • 105,104
  • 32
  • 201
  • 196
MrCurious
  • 333
  • 1
  • 3
  • 6

5 Answers5

38

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.

one_to_hundred = pd.Series(range(1,101))
miradulo
  • 28,857
  • 6
  • 80
  • 93
16
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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sowmya Pai
  • 161
  • 1
  • 2
4

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.

Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
2

try pd.Series([0 for i in range(20)]). It will create a pd series with 20 rows

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
1
num=np.arange(1,101)

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

buddemat
  • 4,552
  • 14
  • 29
  • 49
Shaonsani
  • 178
  • 8