pandas 如何在转置后保持两列值对齐?

bxfogqkk  于 2023-06-04  发布在  其他
关注(0)|答案(1)|浏览(123)

我的数据:

```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)```

我想转置我的数据集的col2,我想保留col1的相应值。我想要的输出:

到目前为止的尝试:
我将其转置如下,但clo1的相应值未显示在输出中。

#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
s8vozzvw

s8vozzvw1#

可以使用pivot_table。关键是使用正则表达式'^\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(''))

输出:

>>> 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

相关问题