簡體   English   中英

如何使用熊貓壓縮多個大小不同的列表?

[英]How do I Zipp several lists with different size using pandas?

我有以下代碼

col1 = [ "Manjeet"] 
col2= [["a"], ["b"], ["c"]] 
col3= [ ["hello"], ["hello"], ["hello"] ] 

我試圖實現這一目標

result = [[ "Manjeet", ["a"], ["hello"]],
          [ "Manjeet", ["b"], ["hello"]],
          [ "Manjeet", ["c"], ["hello"]]]

對於大熊貓,我已經嘗試過使用cicle,但是需要很長時間,有什么建議嗎?

這是一種方法:

pd.DataFrame([col1,col2,col3]).T.ffill() #.values (for converting to array)

         0    1        2
0  Manjeet  [a]  [hello]
1  Manjeet  [b]  [hello]
2  Manjeet  [c]  [hello]

zip_longest + ffill

from itertools import zip_longest
pd.DataFrame(zip_longest(col1, col2, col3)).ffill()

#         0    1         2
#0  Manjeet  [a]   [hello]
#1  Manjeet  [b]   [hello]
#2  Manjeet  [c]   [hello]

對於更長的列表應該更快。


對於平鋪填充,您可以采取類似的方法,只需展開列表,然后最后清理突出部分即可。

import numpy as np

col2 = [['a'], ['b']] # Only 2 elements, so third should be filled with 'a'

cols = [col1, col2, col3]
m = np.array([len(x) for x in cols])
m = np.ceil(m.max()/m).astype(int)

pd.DataFrame(zip_longest(*[x*y for x,y in zip(cols, m)])).dropna()

#         0    1        2
#0  Manjeet  [a]  [hello]
#1  Manjeet  [b]  [hello]
#2  Manjeet  [a]  [hello]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM