假设您可以依靠“col1”来定义组,则可以使用
pivot
使用消除行重复后
cumsum
和
groupby.cumcount
和
groupby.ffill
:
df = (pd.read_csv('input_file.tsv', sep='\t', header=None)
.assign(index=lambda x: x[0].eq('col1').cumsum(),
n=lambda x: x.groupby(['index', 0]).cumcount())
.pivot(index=['index', 'n'], columns=0, values=1)
.groupby(level='index').ffill()
.reset_index(drop=True).rename_axis(columns=None)
)
输出
col1 col2 col3 col4 col5
0 qweqweq 345435 01/01/2024 35:08:09 1 0
1 qweqweq 345435 01/01/2024 35:08:09 0 0
2 qweqweq 345435 01/01/2024 35:08:09 1 1
3 qweqweq 345435 01/01/2024 35:08:09 0 1
可复制输入:
import io
input_file = io.StringIO('''col1\tqweqweq
col2\t345435
col3\t01/01/2024 35:08:09
col4\t1
col5\t0
col4\t0
col5\t0
col4\t1
col5\t1
col4\t0
col5\t1''')
中间体:
# before pivot
0 1 index n
0 col1 qweqweq 1 0
1 col2 345435 1 0
2 col3 01/01/2024 35:08:09 1 0
3 col4 1 1 0
4 col5 0 1 0
5 col4 0 1 1
6 col5 0 1 1
7 col4 1 1 2
8 col5 1 1 2
9 col4 0 1 3
10 col5 1 1 3
# before the cleanup-step:
0 col1 col2 col3 col4 col5
index n
1 0 qweqweq 345435 01/01/2024 35:08:09 1 0
1 qweqweq 345435 01/01/2024 35:08:09 0 0
2 qweqweq 345435 01/01/2024 35:08:09 1 1
3 qweqweq 345435 01/01/2024 35:08:09 0 1