How to Create a Dataframe Using Range in Python

To create a dataframe using range in Python, you can use the range() function.

The following example shows how to create a dataframe using range in Python.

Using range() Function

We can use the range() function to create a dataframe with a specified range of indices.

Suppose we want to create a dataframe with indices ranging from 1 to 5 and columns named “A”, “B”, and “C”:

import pandas as pd

# Use range() function
index = range(1, 6)

# Declare columns
columns = ["A", "B", "C"]

# Create dataframe
df = pd.DataFrame(index=index, columns=columns)

# Show dataframe
print(df)

Output: 👇️

     A    B    C
1  NaN  NaN  NaN
2  NaN  NaN  NaN
3  NaN  NaN  NaN
4  NaN  NaN  NaN
5  NaN  NaN  NaN

In this example, we use the range() function to create the index for the dataframe df. The dataframe has columns “A”, “B”, and “C”, and the index ranges from 1 to 5.

The output shows the dataframe with null values in it.

Conclusion

We can use the range() function to create a dataframe with a specified range of indices in Python.