Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert each cell of column to list using Python

Tags:

python

pandas

I'd like to convert each cell of dataframe to list

import pandas as pd
a=[619, 200, 50, 45]
dic={"a":a}
df = pd.DataFrame.from_dict(dic)

#Output should be like this:
  a
[619]
[200]
[50]
[45] 
like image 805
Ing DESIGN Avatar asked Sep 16 '25 04:09

Ing DESIGN


1 Answers

Use:

df["a"] = df["a"].apply(lambda x: [x])
print(df)

Output

       a
0  [619]
1  [200]
2   [50]
3   [45]

Or:

df["a"] = [[x] for x in df["a"]]
like image 77
Dani Mesejo Avatar answered Sep 17 '25 19:09

Dani Mesejo