我的数据:
```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
1条答案
按热度按时间s8vozzvw1#
可以使用
pivot_table
。关键是使用正则表达式'^\d+\.\d+$'
来标识行:输出: