如何将数据框列转换为列表,并将列表中的值转换为双引号 [英] How to convert a dataframe column to list and values in the list to be enclosed with double quotes

查看:219
本文介绍了如何将数据框列转换为列表,并将列表中的值转换为双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据框中有一个列表列,如下所述.

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

但我希望此格式的输出为["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(',')))

输出-

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

我们用逗号分割,然后将每个元素转换为字符串.

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

这段代码也可以用于获取相同的输出-

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

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

这篇关于如何将数据框列转换为列表,并将列表中的值转换为双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆