Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the pandas dataframe df which contains the list column

0         1

foo       [foo,bar]

bar       [bar,foo]

What is an effective way to add double quotes to specific list element to make the output like this:

0         1

foo       ["foo","bar"]

bar       ["bar","foo"]

Kindly assume that there are more than 2 rows.

1 Answer

0 votes
by (36.8k points)

You can use the list comprehension:

df["1"] = [[f'"{j}"' for j in i] for i in df["1"]]

print (df)

                1

0  ["foo", "bar"]

1  ["bar", "foo"]

Do check out data science with python certification to understand from scratch

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...