Adding empty columns to python DataFrame

144 views Asked by At

I am trying to add couple of empty columns to a python Dataframe , the columns to be added are in the form of list, how could I do it

list=[4,5,6]
data1={'77':[1.2,2.2,3.5],'18':[4.5,6.5,9.9],'89':[8.8,9.8,7.7]}
Input=pd.DataFrame(data1)
Input
data2={'77':[1.2,2.2,3.5],'18':[4.5,6.5,9.9],'89':[8.8,9.8,7.7],'4':[np.NaN,np.NaN,np.NaN],'5':[np.NaN,np.NaN,np.NaN],'6':[np.NaN,np.NaN,np.NaN]}
Output=pd.DataFrame(data2)
Output

What I thought of doing: I cant manually add each column one at a time to dataframe as in my usecase we will be adding 1000+ empty columns, I tried of running through a loop but it dint work out

1

There are 1 answers

2
AudioBubble On

Do this:

List = [4, 5, 6]

data = {'77':[1.2, 2.2, 3.5], '18':[4.5, 6.5, 9.9], '89':[8.8, 9.8, 7.7]}
for i in List: # for create empty columns with column names from the list
     Input[i] = np.nan

Input

Output:

     77  18  89  4   5   6  
0   1.2 4.5 8.8 NaN NaN NaN
1   2.2 6.5 9.8 NaN NaN NaN
2   3.5 9.9 7.7 NaN NaN NaN