Home > Back-end >  Pandas .transform(list) to convert every element in column into singleton list
Pandas .transform(list) to convert every element in column into singleton list

Time:06-15

I want to convert every element x in a pandas df column to [x]. Right now, I'm using df['col'].transform(lambda x: [x]) but it's slow, and I was hoping to speed it up by using df['col'.transform('list') where the list refers to the constructor method, that I hoped, would be faster since it's built into python. This doesn't compile, giving a Transform function failed, and if I use .transform(list) without the single quotes, I get a Function did not transform error instead. I also get errors when trying to use apply().

CodePudding user response:

IIUC, you are probably looking for every element (not just one column) to be made as list:

df = df.applymap(lambda x: [x])

CodePudding user response:

One way using numpy.reshape and tolist:

df = pd.DataFrame({"col": np.arange(1000)})
df["col"] = df["col"].values.reshape(-1, 1).tolist()

Output:

print(df.head())
   col
0  [0]
1  [1]
2  [2]
3  [3]
4  [4]

Benchmark (~4x faster):

%timeit df["col"].values.reshape(-1, 1).tolist()
# 50.9 µs ± 770 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

%timeit df["col"].transform(lambda x: [x])
# 188 µs ± 2.1 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
  • Related