简体   繁体   English

如何将数据框列转换为列表,并将列表中的值转换为双引号

[英]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.

相关问题 DataFrame 列包含双引号内的列表 - DataFrame column containing list within double quotes 如何用双引号列表转换单引号列表 - how to convert single quotes list with double quotes list 将数据框列值转换为列表 - Convert dataframe column values into a list 如何将Python列表中带单引号的字符串元素转为双引号 - How to convert string elements with single quotes to double quotes in a Python list 如何为 Pandas 数据框中的列表列中的每个元素添加双引号? - How do I add double quotes to every element in a list column in a pandas dataframe? 如何将逗号分隔的字符串转换为用引号''括起来的每个单词到 python 中的列表? - How to convert a comma separated string with each word enclosed in quotes ' ' to a list in python? 如何将逗号分隔的字符串用引号括起来,用空格转换为带有单独元素的列表 - How to convert comma separated string enclosed in quotes, with spaces to a list with separate elements 如何将 DataFrame 列 (len(list) == len(df)) 的列表转换为相应列表 idx 处的列值列表? - How to convert a list of DataFrame columns (len(list) == len(df)) into a list of column values at the respective list idx? 如何将带有值列表的列转换为 Pandas DataFrame 中的行 - How to convert column with list of values into rows in Pandas DataFrame 如何将 append 值列表转换为 dataframe 中列表的一列 - How to append list of values to a column of list in dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM