简体   繁体   中英

How to convert a dataframe column to list and values in the list to be enclosed with double quotes

I have a list column in dataframe as mentioned below.

 df=pd.DataFrame({'a':["a,b,c"]})

df:

a
0  a,b,c
df.a.astype(str).values.tolist()

['a,b,c']

But I want the output to be ["a","b","c"] in this format. Can someone help me with the code.

The following code will result in the desired output -

df.a.apply(lambda x: str(x.split(',')))

Output -

['a', 'b', 'c']

We split on comma and then convert every element to string.

Edit 1 :

This piece of code can also be used to get the same output -

df['a']=[str(i.split(',')) for i in df.a]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM