Ing DESIGN
Ing DESIGN

Reputation: 61

Convert each cell of column to list using Python

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] 

Upvotes: 4

Views: 1361

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61930

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"]]

Upvotes: 4

Related Questions