[英]How to combine multiple lists of string columns in python?
我有一個Python Pandas數據框。
我嘗試創建一個新列total_str
,它是colA
和colB
中的值的列表。
這是預期的輸出:
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']
#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)
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 []
我假設您要在數據numpy.nan
處理numpy.nan
和None
。 您可以簡單地編寫一個輔助函數,以在創建新列時將它們替換為空列表。 這不是干凈的,但可以。
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)
使用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
您可以像這樣在熊貓中添加列:
dataframe['total_str'] = dataframe['colA'] + dataframe['colB']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.