2

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.

0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

df[1] = np.nan doesn't change the size of the data frame and when I use df[len(df.columns)] = np.nan it gives the following error: AttributeError: 'Series' object has no attribute 'columns'
@aim_bee.1 - Answer was edited, use df = s.to_frame() before my solution, because Series:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.