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 OverflowPandas
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.
Top answer 1 of 11
193
Index is an object, and default index starts from 0:
>>> result.index
Int64Index([0, 1, 2], dtype=int64)
You can shift this index by 1 with
>>> result.index += 1
>>> result.index
Int64Index([1, 2, 3], dtype=int64)
2 of 11
41
Just set the index before writing to CSV.
df.index = np.arange(1, len(df) + 1)
And then write it normally.
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....
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 ...
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).
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