How to add an empty column in a dataframe using pandas (without specifying column names)?

Viewed 148

I have a dataframe with only one column (headerless). I want to add another empty column to it having the same number of rows.

To make it clearer, currently, the size of my data frame is 1050 (since only one column), I want the new size to be 1050*2 with the second column being completely empty.

1 Answers

In pandas in DataFrame are always columns, so for new default column filled by missing values use length of columns:

s = pd.Series([2,3,4])

df = s.to_frame()

df[len(df.columns)] = np.nan
#what is same for one column df like
#df[1] = np.nan
print (df)
   0   1
0  2 NaN
1  3 NaN
2  4 NaN
Related