user11877626
user11877626

Reputation:

How to create extra column in dataframe based on length of dataframe as an index value

i am trying to create extra column in dataframe based on length of dataframe as an index value for dataframe

i am trying to do something like this

x = len(df3.columns)
df3['x+1'] = 'N/A'

It does create dataframe but not with index value 4 but with index value x+1

                          1  x+1  
        0          Bag (BG)  N/A  
        1        100.0 Each  N/A  
        2       0.0 / 0.001  N/A  
        3              0.01  N/A  
        4  4.0 x 3.5 x 1.25  N/A  
        5    10888277689268  N/A  

I expect in place of X+1 it should be the number next to 1

Upvotes: 3

Views: 120

Answers (1)

jezrael
jezrael

Reputation: 862601

If need integer column simply remove '':

x = len(df3.columns)
df3[x + 1] = 'N/A'

If need string column one possible solution is use f-strings:

df3[f'{x + 1}'] = 'N/A'

Upvotes: 2

Related Questions