Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Modify a column in Python such that the numbering is continuous

I have a dataset given as such:

#Load the required libraries
import pandas as pd


#Create dataset
data = {'team': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'],
        'Run_time': [1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3, 4],
        'Married': ['No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No'],
        'Self_Employed': ['No', 'No', 'Yes', 'No', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'No'],
        'LoanAmount': [123, 128, 66, 120, 141, 52,96,15,85,36,58,89],
        }

#Convert to dataframe
df = pd.DataFrame(data)
print("df = \n", df)

The dataset looks as such:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here, in the ‘Run_time’ column, the numbering starts at different index values.

I wish to ensure that the ‘Run_time’ column starts from 1 only.

The dataset needs to look as such:

enter image description here

Can somebody please let me know how to modify this column in Python such that the numbering is continuous?

>Solution :

import pandas as pd


#Create dataset
data = {'team': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'],
        'Run_time': [1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3, 4],
        'Married': ['No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'Yes', 'No'],
        'Self_Employed': ['No', 'No', 'Yes', 'No', 'No', 'No', 'Yes', 'No', 'No', 'Yes', 'No', 'No'],
        'LoanAmount': [123, 128, 66, 120, 141, 52,96,15,85,36,58,89],
        }

#Convert to dataframe
df = pd.DataFrame(data)
# print("df = \n", df)
df.Run_time = df.index+1
df
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading