How can I keep both column value aligned after transposing?

Question:

My Data:

```data = {
    'Col1': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
    'Col2': ['33.5', 'W', 'A to B, OK', 'slinks down to hammer', 'T c V b Rell 10 (82b 6x1) DW: 84.14', '33.4', '•', 'A to B, no', 'Tosses it uo', '33.3', 2, 'A to B, 2 R', 'On a right way', 'slinks down to hammer', 'BAN: 185/4CRR: 5.60', 'T 69 (80b 6x4)', 'Mu 7 (17b)', 'Mark 6-0-29-1', 'George Dockrel', 'Bet 31', '33.2', 2, 'A to T, 2 R', 'slinks down to hammer', '33.1', 2, 'A to T, 2 r', 'angling away, cuts it',
            '33.5', 'W', 'A to B, OK', 'slinks down to hammer', 'T c V b Rell 10 (82b 6x1) DW: 84.14', '33.4', '•', 'A to B, no', 'Tosses it uo', '33.3', 2, 'A to B, 2 R', 'On a right way', 'slinks down to hammer', 'BAN: 185/4CRR: 5.60', 'T 69 (80b 6x4)', 'Mu 7 (17b)', 'Mark 6-0-29-1', 'George Dockrel', 'Bet 31', '33.2', 2, 'A to T, 2 R', 'slinks down to hammer', '33.1', 2, 'A to T, 2 r', 'angling away, cuts it']
}

df = pd.DataFrame(data)```

I want to transpose col2 of my dataset and I want to keep the corresponding value from col1.
My desired output:
enter image description here

My try so far:

I transposed it as below but the corresponding value from clo1 is not showing in my output.

#make a list
column_data = df['Col2'].tolist()

# Make overs float
column_data2 = []
for item in column_data:
    if isinstance(item, str) and item.replace('.', '', 1).isdigit():
        column_data2.append(float(item))
    else:
        column_data2.append(item)

df2 = pd.DataFrame(column_data2, columns=['Col2'])

# splits rows based on floats
rows = (df2.Col2.map(type)==float).cumsum()

df3 = df2.groupby(rows).agg(list)
    .Col2.astype(str).str[1:-1]
        .str.split(',', expand=True)
            .add_prefix("col_")

df3
Asked By: new_bee

||

Answers:

You can use pivot_table. The key is to identify rows using a regex '^d+.d+$':

row = df['Col2'].str.contains(r'^d+.d+$').fillna(False).cumsum()
col = df.groupby(row).cumcount()

out = (df.pivot_table(index=['Col1', row], columns=col, values='Col2', aggfunc='first')
         .droplevel(1).reset_index().fillna(''))

Output:

>>> out
    Col1     0  1            2                      3                                    4                    5               6           7              8               9      10
0      1  33.5  W   A to B, OK  slinks down to hammer  T c V b Rell 10 (82b 6x1) DW: 84.14                                                                                        
1      1  33.4  •   A to B, no           Tosses it uo                                                                                                                             
2      1  33.3  2  A to B, 2 R         On a right way                slinks down to hammer  BAN: 185/4CRR: 5.60  T 69 (80b 6x4)  Mu 7 (17b)  Mark 6-0-29-1  George Dockrel  Bet 31
3      1  33.2  2  A to T, 2 R  slinks down to hammer                                                                                                                             
4      1  33.1  2  A to T, 2 r  angling away, cuts it                                                                                                                             
5      1  33.5  W                                                                                                                                                                 
6      2            A to B, OK  slinks down to hammer  T c V b Rell 10 (82b 6x1) DW: 84.14                                                                                        
7      2  33.4  •   A to B, no           Tosses it uo                                                                                                                             
8      2  33.3  2  A to B, 2 R         On a right way                slinks down to hammer  BAN: 185/4CRR: 5.60  T 69 (80b 6x4)  Mu 7 (17b)  Mark 6-0-29-1  George Dockrel  Bet 31
9      2  33.2  2  A to T, 2 R  slinks down to hammer                                                                                                                             
10     2  33.1  2  A to T, 2 r  angling away, cuts it                                                                                                                             
Answered By: Corralien
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.