如何将一列转换为多行以获取python中的列值?

问题描述 投票:2回答:2

如果我有一个数据框,并且它的col1是文件名,那么col2是我想将其值转置为文件名的值,例如:

Input:
col1  col2
file1 text_0
file1 text_1
file1 text_2
file2 text_0
file2 text_1
file2 text_2
file2 text_3
file3 text_0

Output:
col1  col2   col3   col4   col3
file1 text_0 text_1 text_2 
file2 text_0 text_1 text_2 text_3
file3 text_0 
python pandas dataframe reshape transpose
2个回答
0
投票
似乎您具有DataFrames,这意味着您正在使用Pandas。考虑根据您的实际需要检查pandas.transposepandas.pivot

0
投票
尝试一下:

>>> pivoted_df = df.pivot(index='col1',columns = 'col2', values='col2').fillna('') >>> pivoted_df.columns = [f'col{i}' for i,_ in enumerate(df.columns,start=2)] >>> pivoted_df col2 col3 col4 col3 col1 file1 text_0 text_1 text_2 file2 text_0 text_1 text_2 text_3 file3 text_0

我不了解列名背后的逻辑,所以找不到模式。
© www.soinside.com 2019 - 2024. All rights reserved.