Add column of empty lists to DataFrame

EDIT: the commenters caught the bug in my answer

s = pd.Series([[]] * 3)
s.iloc[0].append(1) #adding an item only to the first element
>s # unintended consequences:
0    [1]
1    [1]
2    [1]

So, the correct solution is

s = pd.Series([[] for i in range(3)])
s.iloc[0].append(1)
>s
0    [1]
1     []
2     []

OLD:

I timed all the three methods in the accepted answer, the fastest one took 216 ms on my machine. However, this took only 28 ms:

df['empty4'] = [[]] * len(df)

Note: Similarly, df['e5'] = [set()] * len(df) also took 28ms.


One more way is to use np.empty:

df['empty_list'] = np.empty((len(df), 0)).tolist()

You could also knock off .index in your "Method 1" when trying to find len of df.

df['empty_list'] = [[] for _ in range(len(df))]

Turns out, np.empty is faster...

In [1]: import pandas as pd

In [2]: df = pd.DataFrame(pd.np.random.rand(1000000, 5))

In [3]: timeit df['empty1'] = pd.np.empty((len(df), 0)).tolist()
10 loops, best of 3: 127 ms per loop

In [4]: timeit df['empty2'] = [[] for _ in range(len(df))]
10 loops, best of 3: 193 ms per loop

In [5]: timeit df['empty3'] = df.apply(lambda x: [], axis=1)
1 loops, best of 3: 5.89 s per loop

Tags:

Python

Pandas