如何在python中组合多个字符串列列表?

时间:2017-05-17 03:37:42

标签: python list pandas dataframe

我有一个Python Pandas数据帧。

我尝试创建一个新列AddNewData,该列是total_strcolA中的值列表。

这是预期的输出:

colB

5 个答案:

答案 0 :(得分:2)

#replace nan with empty list and then concatenate colA and colB using sum.
df['total_str'] = df.applymap(lambda x: [] if x is np.nan else x).apply(lambda x: sum(x,[]), axis=1)

df
Out[705]: 
        colA       colB           total_str
0  [a, b, c]  [a, b, c]  [a, b, c, a, b, c]
1  [a, b, c]        NaN           [a, b, c]
2  [a, b, c]     [d, e]     [a, b, c, d, e]

如果DF中有其他列,您可以使用:

df['total_str'] = df.applymap(lambda x: [] if x is np.nan else x).apply(lambda x: x.colA+x.colB, axis=1)

答案 1 :(得分:1)

chain为你做这个伎俩。

itertools.chain(*filter(bool, [colA, colB]))

这将返回一个迭代器,如果需要,可以使用list结果来获取列表,例如

import itertools

def test(colA, colB):
    total_str = itertools.chain(*filter(bool, [colA, colB]))
    print list(total_str)


test(['a', 'b'], ['c'])  # output: ['a', 'b', 'c']
test(['a', 'b', 'd'], None)  # output: ['a', 'b', 'c']
test(['a', 'b', 'd'], ['x', 'y', 'z'])  # ['a', 'b', 'd', 'x', 'y', 'z']
test(None, None)  # output []

答案 2 :(得分:0)

我假设您要在数据框中处理numpy.nanNone。在创建新列时,您可以简单地编写一个辅助函数来用空列表替换它们。它不干净但有效。

def helper(x):
    return x if x is not np.nan and x is not None else []

dataframe['total_str'] = dataframe['colA'].map(helper) + dataframe['colB'].map(helper)

答案 3 :(得分:0)

使用combine_first替换NaN以清空list以获得更快的解决方案:

df['total_str'] = df['colA'] + df['colB'].combine_first(pd.Series([[]], index=df.index))
print (df)
        colA       colB           total_str
0  [a, b, c]  [a, b, c]  [a, b, c, a, b, c]
1  [a, b, c]        NaN           [a, b, c]
2  [a, b, c]     [d, e]     [a, b, c, d, e]
df['total_str'] = df['colA'].add(df['colB'].combine_first(pd.Series([[]], index=df.index)))
print (df)
        colA       colB           total_str
0  [a, b, c]  [a, b, c]  [a, b, c, a, b, c]
1  [a, b, c]        NaN           [a, b, c]
2  [a, b, c]     [d, e]     [a, b, c, d, e]

<强>计时

df = pd.DataFrame({'colA': [['a','b','c']] * 3,  'colB':[['a','b','c'], np.nan, ['d','e']]})
#[30000 rows x 2 columns]
df = pd.concat([df]*10000).reset_index(drop=True)
#print (df)
In [62]: %timeit df['total_str'] = df['colA'].combine_first(pd.Series([[]], index=df.index)) + df['colB'].combine_first(pd.Series([[]], index=df.index))
100 loops, best of 3: 8.1 ms per loop

In [63]: %timeit df['total_str1'] = df['colA'].fillna(pd.Series([[]], index=df.index)) + df['colB'].fillna(pd.Series([[]], index=df.index))
100 loops, best of 3: 9.1 ms per loop

In [64]: %timeit df['total_str2'] = df.applymap(lambda x: [] if x is np.nan else x).apply(lambda x: x.colA+x.colB, axis=1)
1 loop, best of 3: 960 ms per loop

答案 4 :(得分:-1)

你可以像这样在pandas中添加列:

dataframe['total_str'] = dataframe['colA'] + dataframe['colB']