[英]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: 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. 但我希望此格式的输出为[“ a”,“ b”,“ c”]。 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 : 编辑1:
This piece of code can also be used to get the same output - 这段代码也可以用来获取相同的输出-
df['a']=[str(i.split(',')) for i in df.a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.