Use range(df):

idx = 0
df.insert(idx, 'index', value=range(len(df)))

Or for better performance:

df.insert(idx, 'index', value=np.arange(len(df)))

If want add column to last one:

df = df.assign('index', value=np.arange(len(df)))

df['index'] = np.arange(len(df)

But if want select this column is necessary use []:

print (df['index'])

because with dot notation it select index:

print (df.index)
Answer from jezrael on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.set_index.html
pandas.DataFrame.set_index — pandas 3.0.3 documentation
Here, “array” encompasses Series, Index, np.ndarray, and instances of Iterator. ... Delete columns to be used as the new index.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Set a column as the DataFrame index with set_index() | note.nkmk.me
January 26, 2024 - pandas.DataFrame.swaplevel — ... index is removed. To keep the original index as a column, use reset_index() to renumber the index starting from 0....
🌐
PYnative
pynative.com › home › python › pandas › reset index in pandas dataframe
Reset index in pandas DataFrame
March 9, 2023 - We can use DataFrame.reset_index() to reset the index of the updated DataFrame. By default, it adds the current row index as a new column called ‘index’ in DataFrame, and it will create a new row index as a range of numbers starting at 0.
🌐
Medium
medium.com › @petehouston › set-index-for-dataframe-in-pandas-55400e306e42
Set index for DataFrame in pandas | by Pete Houston | Medium
March 20, 2019 - ... It is auto-generated index column, because pandas always tries to optimize every dataset it handles, so it generated. However, that auto-generated index field starts from 0 and unnamed.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-start-index-at-1-for-pandas-dataframe
How to Start Index at 1 for Pandas DataFrame | Saturn Cloud Blog
June 19, 2023 - In this example, we use the range() function to create a range of values starting from 1 and ending at the length of the DataFrame plus 1. We then assign this range to the index attribute of the DataFrame. This method has the advantage of not creating a new column for the original index, but it requires more code than the previous method. If you want more control over the index values, you can create a custom index using the Index class: import pandas as pd # create a sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # create a custom index starting from 1 index = pd.Index(range(1, len(df)+1)) # assign the custom index to the DataFrame df.index = index print(df)
🌐
PYnative
pynative.com › home › python › pandas › set index in pandas dataframe
Set index in pandas DataFrame
March 9, 2023 - DataFrame is the tabular structure ... an index, whereas column label is called column index/header. By default, while creating DataFrame, Python pandas assign a range of numbers (starting at 0) as a row index....
🌐
Saturn Cloud
saturncloud.io › blog › how-to-set-the-first-column-and-row-as-index-in-pandas
How to Set the First Column and Row as Index in Pandas | Saturn Cloud Blog
June 19, 2023 - We then used the read_csv() function ... and row as index in pandas, we can use a combination of the set_index() method and the header and index_col arguments of the read_csv() function....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › set-the-first-column-and-row-as-index-in-pandas
Set the First Column and Row as Index in Pandas - GeeksforGeeks
July 23, 2025 - Original DataFrame: 0 1 2 0 Name Age City 1 Alice 25 New York 2 Bob 30 San Francisco 3 Charlie 35 Los Angeles DataFrame after removing the first row (header): 0 Name Age City 1 Alice 25 New York 2 Bob 30 San Francisco 3 Charlie 35 Los Angeles · You can also set both the first column as the index and the first row as the column names. Consider this DataFrame: ... import pandas as pd data = [['Name', 'Alice', 'Bob', 'Charlie'], ['Age', 25, 30, 35], ['City', 'New York', 'San Francisco', 'Los Angeles']] df = pd.DataFrame(data) print("Original DataFrame:") print(df) df.columns = df.iloc[0] df = df[1:] df.set_index('Name', inplace=True) print("\nDataFrame after removing the first row (header) and setting 'Name' column as index:") print(df)
🌐
Pandas
pandas.pydata.org › docs › user_guide › indexing.html
Indexing and selecting data — pandas 3.0.3 documentation
In both cases, start and stop determine the label boundaries (inclusive), while step skips positions within that range, regardless of the index type. pandas provides a suite of methods in order to get purely integer based indexing. The semantics follow closely Python and NumPy slicing. These are 0-based indexing.
🌐
Aporia
aporia.com › home › how to convert the index of a dataframe to a column
How to Convert Index to Column in Pandas DataFrame - Aporia
September 4, 2024 - In this short how-to article, we will learn how to create a column from the index of Pandas DataFrames. The reset_index function resets the index of the DataFrame with the default index which is integers starting from 0 and creates a column ...
Rating: 5 ​ - ​ 3 votes
🌐
Codegive
codegive.com › blog › pandas_add_index_column_start_from_1.php
Pandas Add Index Column Start from 1 (2026): Master Data Sequencing & Unlock Hidden DataFrame Power
When you create a new column directly from df.index or use functions like cumcount(), they naturally start counting from 0. To pandas add index column start from 1, you simply need to add + 1 to the 0-based result (e.g., df.index + 1 or df.groupby().cumcount() + 1).
🌐
Built In
builtin.com › data-science › pandas-add-column
How to Add Columns in a Pandas DataFrame | Built In
| Image: Soner Yildirim · The insert function takes three parameters that are the index, the name of the column and the values. The column indices start from zero, so we set the index parameter as one to add the new column next to column A.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › pandas-dataframe-index
Pandas Dataframe Index - GeeksforGeeks
March 24, 2026 - import pandas as pd data = {'Name': ... RangeIndex(start=0, stop=3, step=1) The set_index() method is used to change the index of a DataFrame by setting one or more columns as the new index....
🌐
Statology
statology.org › home › pandas: how to use first column as index
Pandas: How to Use First Column as Index
August 19, 2022 - If we import the CSV file without specifying an index column, pandas will simply create an index column with numerical values starting at 0:
🌐
GeeksforGeeks
geeksforgeeks.org › python › reset-index-in-pandas-dataframe
Reset Index in Pandas Dataframe - GeeksforGeeks
October 3, 2025 - If a DataFrame has a non-continuous or unordered index, you can reset it to a default integer index starting from 0. For example, suppose the current DataFrame has this index: [0, 2, 4, 5, 7], after resetting the index, it becomes [0, 1, 2, ...
🌐
Python Examples
pythonexamples.org › pandas-set-column-as-index
Set Column as Index in Pandas DataFrame - Examples
import pandas as pd #initialize a dataframe df = pd.DataFrame( [[21, 'Amol', 72, 67], [23, 'Lini', 78, 69], [32, 'Kiku', 74, 56], [52, 'Ajit', 54, 76]], columns=['rollno', 'name', 'physics', 'botony']) print('DataFrame with default index\n', df) #set multiple columns as index df = df.set_index(['rollno','name']) print('\nDataFrame with MultiIndex\n',df) D:\>python example1.py DataFrame with default index rollno name physics botony 0 21 Amol 72 67 1 23 Lini 78 69 2 32 Kiku 74 56 3 52 Ajit 54 76 DataFrame with MultiIndex physics botony rollno name 21 Amol 72 67 23 Lini 78 69 32 Kiku 74 56 52 Ajit 54 76
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas set index to column in dataframe
Pandas Set Index to Column in DataFrame - Spark By {Examples}
November 14, 2024 - In order to set the index to a column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column.