How to add an empty column to a dataframe?
This is partially covered already.
The dtype of df["D"] = np.nan
in the accepted answer is dtype=numpy.float64
.
Is there a way to initialize an empty list into each cell?
Tried df["D"] = [[]] * len(df)
but all values are pointing to the same object and setting one to a value sets them all.
df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
df
A B
0 1 2
1 2 3
2 3 4
df["D"] = [[]] * len(df)
df
A B D
0 1 2 []
1 2 3 []
2 3 4 []
df['D'][1].append(['a','b','c','d'])
df
A B D
0 1 2 [[a, b, c, d]]
1 2 3 [[a, b, c, d]]
2 3 4 [[a, b, c, d]]
wanted
A B D
0 1 2 []
1 2 3 [[a, b, c, d]]
2 3 4 []