Home > Mobile >  in python how can i add a column (next to the index column) with the values 1,2,3,4,etc until the en
in python how can i add a column (next to the index column) with the values 1,2,3,4,etc until the en

Time:01-05

    location      date      
0   Afghanistan 24-02-20    
1   Afghanistan 25-02-20    
2   Afghanistan 26-02-20    
3   Afghanistan 27-02-20    
4   Afghanistan 28-02-20    

CodePudding user response:

Assuming, the above is a DataFrame, you can use the following to obtain the result:

df.insert(0, 'ID', range(1, 1 len(df)))

CodePudding user response:

Use the following code:

Locations = ['Afghanistan','USA','Canada','France','UK']
Dates = ['24-02-20','25-02-20','26-02-20','27-02-20','28-02-20']

for i in range(len(list(zip(Locations,Dates)))):
  print(f"{i} {Locations[i]} {Dates[i]}")

zip() prevents bugs if a list is longer than the other

Output:

0 Afghanistan 24-02-20
1 USA 25-02-20
2 Canada 26-02-20
3 France 27-02-20
4 UK 28-02-20
  •  Tags:  
  • Related