1

Given a Data Frame like the following:

df = pd.DataFrame({'term' : ['analys','applic','architectur','assess','item','methodolog','research','rs','studi','suggest','test','tool','viewer','work'],
               'newValue' : [0.810419, 0.631963 ,0.687348, 0.810554, 0.725366, 0.742715, 0.799152, 0.599030, 0.652112, 0.683228, 0.711307, 0.625563,  0.604190, 0.724763]})

df = df.set_index('term')

print(df)

             newValue
term                 
analys       0.810419
applic       0.631963
architectur  0.687348
assess       0.810554
item         0.725366
methodolog   0.742715
research     0.799152
rs           0.599030
studi        0.652112
suggest      0.683228
test         0.711307
tool         0.625563
viewer       0.604190
work         0.724763

I want to add n new empty columns "". Therefore, I have a value stored in variable n which indicates the number of required new columns.

n = 5

Thanks for your help in advance!

Pete
  • 100
  • 4
  • 15

2 Answers2

2

According to this answer,

Each not empty DataFrame has columns, index and some values.

So your dataframe must not have a column without name anyway.

This is the shortest way that I know of to achieve your goal:

n = 5
for i in range(n):
  df[len(df.columns)] = ""

                newValue    1   2   3   4   5
term                        
analys          0.810419                    
applic          0.631963                    
architectur     0.687348                    
assess          0.810554                    
item            0.725366                    
methodolog      0.742715                    
research        0.799152                    
rs              0.599030                    
studi           0.652112                    
suggest         0.683228                    
test            0.711307                    
tool            0.625563                    
viewer          0.604190                    
work            0.724763                    
TQA
  • 267
  • 3
  • 12
1

IIUC, you can use:

n= 5
df=(pd.concat([df,pd.DataFrame(columns=['col'+str(i)
               for i in range(n)])],axis=1,sort=False).fillna(''))
print(df)

             newValue col0 col1 col2 col3 col4 col0 col1 col2 col3 col4
analys       0.810419                                                  
applic       0.631963                                                  
architectur  0.687348                                                  
assess       0.810554                                                  
item         0.725366                                                  
methodolog   0.742715                                                  
research     0.799152                                                  
rs           0.599030                                                  
studi        0.652112                                                  
suggest      0.683228                                                  
test         0.711307                                                  
tool         0.625563                                                  
viewer       0.604190                                                  
work         0.724763                 

Note: You can remove the fillna() if you want NaN.

anky
  • 74,114
  • 11
  • 41
  • 70