列表列,将列表转换为字符串作为新列 [英] Column of lists, convert list to string as a new column

查看:530
本文介绍了列表列,将列表转换为字符串作为新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一列列表的数据框,可以使用以下列表创建该列:

I have a dataframe with a column of lists which can be created with:

import pandas as pd
lists={1:[[1,2,12,6,'ABC']],2:[[1000,4,'z','a']]}
#create test dataframe
df=pd.DataFrame.from_dict(lists,orient='index')
df=df.rename(columns={0:'lists'})

数据帧df如下:

                lists
1  [1, 2, 12, 6, ABC]
2     [1000, 4, z, a]

我需要创建一个名为"liststring"的新列,该列将使用lists中每个列表的每个元素,并创建一个字符串,每个元素之间用逗号分隔.每个列表的元素可以是intfloatstring.因此结果将是:

I need to create a new column called 'liststring' which takes every element of each list in lists and creates a string with each element separated by commas. The elements of each list can be int, float, or string. So the result would be:

                lists    liststring
1  [1, 2, 12, 6, ABC]  1,2,12,6,ABC
2     [1000, 4, z, a]    1000,4,z,a

我尝试了各种方法,包括从转换熊猫DF列表成字符串:

I have tried various things, including from Converting a Panda DF List into a string:

df['liststring']=df.lists.apply(lambda x: ', '.join(str(x)))

但不幸的是,结果会占用每个字符并以逗号分隔:

but unfortunately the result takes every character and seperates by comma:

                lists                                         liststring
1  [1, 2, 12, 6, ABC]  [, 1, ,,  , 2, ,,  , 1, 2, ,,  , 6, ,,  , ', A...
2     [1000, 4, z, a]  [, 1, 0, 0, 0, ,,  , 4, ,,  , ', z, ', ,,  , '...

提前感谢您的帮助!

推荐答案

列表理解

如果性能很重要,我强烈建议您使用此解决方案,并且解释原因.

df['liststring'] = [','.join(map(str, l)) for l in df['lists']]
df

                lists    liststring
0  [1, 2, 12, 6, ABC]  1,2,12,6,ABC
1     [1000, 4, z, a]    1000,4,z,a

您可以使用函数将其扩展到更复杂的用例.

You can extend this to more complicated use cases using a function.

def try_join(l):
    try:
        return ','.join(map(str, l))
    except TypeError:
        return np.nan

df['liststring'] = [try_join(l) for l in df['lists']]


Series.apply/Series.agg','.join

您需要先将列表项转换为字符串,这是map派上用场的地方.


Series.apply/Series.agg with ','.join

You need to convert your list items to strings first, that's where the map comes in handy.

df['liststring'] = df['lists'].apply(lambda x: ','.join(map(str, x)))

或者

df['liststring'] = df['lists'].agg(lambda x: ','.join(map(str, x)))

df
                lists    liststring
0  [1, 2, 12, 6, ABC]  1,2,12,6,ABC
1     [1000, 4, z, a]    1000,4,z,a


具有DataFrame.agg

pd.DataFrame构造函数

非循环/非lambda解决方案.


pd.DataFrame constructor with DataFrame.agg

A non-loopy/non-lambda solution.

df['liststring'] = (
 pd.DataFrame(df.lists.tolist())
   .fillna('')
   .astype(str)
   .agg(','.join, 1)
   .str.strip(',')
)

df
                lists    liststring
0  [1, 2, 12, 6, ABC]  1,2,12,6,ABC
1     [1000, 4, z, a]    1000,4,z,a

这篇关于列表列,将列表转换为字符串作为新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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