python - Pandas /Python : How to Transpose Duplicate Rows to Columns and Preserve Order?

标签 python pandas

我有两列。 第一列具有值 A、B、C、D,第二列值对应于 A、B、C 和 D。

我想将 A、B、C 和 D 转换/转置为名为 A、B、C、D 的 4 列,并具有之前对应于 A、B、C、D 的任何值(原始第二列)在相应列下排序 - A、B、C 或 D。必须保留原始顺序。

这是一个例子。

Input:

A|1
B|2
C|3
D|4
A|3
B|6
C|3
D|6

Desired output:
A|B|C|D
1|2|3|4
3|6|3|6

关于如何使用 Pandas/Python 完成此任务有什么想法吗?

非常感谢!

最佳答案

与两列旋转非常相似 ( Q/A 10 here ):

(df.assign(idx=df.groupby('col1').cumcount())
   .pivot(index='idx', columns='col1', values='col2')
)

输出:

col1  A  B  C  D
idx             
0     1  2  3  4
1     3  6  3  6

关于python - Pandas /Python : How to Transpose Duplicate Rows to Columns and Preserve Order?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66792082/

相关文章:

python - 如何对大型词典进行排序

python - 从应用程序代码内部使用 Alembic API

python - Pandas 数据框的高效计算

python - Pandas 替换 DataFrame 中的第一个结果

python - 将数据帧列分解为多行(TypeError : Cannot cast array data from dtype ('int64' ) to dtype ('int32' ))

python - 具有复杂结构的数据类实例的完整副本

python - Pandas 数据框分组并检索日期范围

python - 从包含正斜杠 python 数据帧的列中删除行

python - ( Pandas )如何计算与以前相同的值出现的频率? (并从中提取一个新的列)

Python合并两个字典