0

Despite the insane number of list questions on this website, I still haven't been able to get this right.

First element in the list is another list. Trying to break that out and then populate it vertically.

have = [[['v', 'e', 'r', 't'], 'A', 'B', 'C', 'D'],
        [['v', 'e', 'r', 't'], 'E', 'F', 'G', 'H']]

want = [['v', 'A', 'B', 'C', 'D'],
        ['e', 'A', 'B', 'C', 'D'],
        ['r', 'A', 'B', 'C', 'D'],
        ['t', 'A', 'B', 'C', 'D'],
        ['v', 'E', 'F', 'G', 'H'],
        ['e', 'E', 'F', 'G', 'H'],
        ['r', 'E', 'F', 'G', 'H'],
        ['t', 'E', 'F', 'G', 'H']]
Josh
  • 1,493
  • 1
  • 13
  • 24

1 Answers1

1

Use this:

have = [[['v', 'e', 'r', 't'], 'A', 'B', 'C', 'D'],
    [['v', 'e', 'r', 't'], 'E', 'F', 'G', 'H']]
df = pd.DataFrame(have)

(df[0].apply(pd.Series).stack().reset_index(level=1, drop=True)
     .to_frame()
     .merge(df, left_index=True, right_index=True,suffixes=('','_r'))
     .drop('0_r',axis=1).values.tolist())

Output:

[['v', 'A', 'B', 'C', 'D'],
 ['e', 'A', 'B', 'C', 'D'],
 ['r', 'A', 'B', 'C', 'D'],
 ['t', 'A', 'B', 'C', 'D'],
 ['v', 'E', 'F', 'G', 'H'],
 ['e', 'E', 'F', 'G', 'H'],
 ['r', 'E', 'F', 'G', 'H'],
 ['t', 'E', 'F', 'G', 'H']]
Scott Boston
  • 147,308
  • 15
  • 139
  • 187