Generating numbers
Generating numbers is not something special to pandas
, rather numpy
module or range
function (as mentioned by @Grismer) can do the trick. Let's say you want to generate a series of numbers and assign these numbers to a dataframe. As I said before, there are multiple approaches two of which I personally prefer.
Take range(1,1000,1)
as an Example. This function gets three arguments two of which are not mandatory. The first argument defines the start number, the second one defines the end number, and the last one points to the steps of this range. So the abovementioned example will result in the numbers 1 to 9999 (Note that this range is a half-open interval which is closed at the start and open at the end).
To have the same results as the previous example, take numpy.arange(1,1000,1)
as an example. The arguments are completely the same as the range
's arguments.
Assigning to dataframe
Now, if you want to assign these numbers to a dataframe, you can easily do this by using the pandas
module. Code below is an example of how to generate a dataframe:
import numpy as np
import pandas as pd
myRange = np.arange(1,1001,1) # Could be something like myRange = range(1,1000,1)
df = pd.DataFrame({"numbers": myRange})
df.head(5)
which results in a dataframe like(Note that just the first five rows have been shown):
|
numbers |
0 |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
Difference of numpy.arange
and range
To keep this answer short, I'd rather to refer to this answer by @hpaulj