tentativa123
tentativa123

Reputation: 31

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

i've got the following code

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

and im trying to achieve this

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

With pandas, i've tried For cicle but it takes to long, Any suggestion?

Upvotes: 1

Views: 69

Answers (2)

ALollz
ALollz

Reputation: 59549

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]

Should be faster for longer lists.


For a tiled filling you can take a similar approach, just expand the lists, then clean up the overhang in the end.

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]

Upvotes: 1

anky
anky

Reputation: 75080

Here is one way:

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]

Upvotes: 1

Related Questions