python - 在python中转置数据

标签 python python-2.7 python-3.x numpy pandas

我的数据看起来像这样

cust c1 p1 c2 p2 c3 p3
1    2  3  4  5  6  7
8    9  10 11 12 13 14

我想转置这些数据并以下面的格式返回:

cust c  p
1    2  3
1    4  5
1    6  7
8    9  10
8    11 12
8    13 14

请有人可以建议使用什么?

最佳答案

您可以将函数 lreshapesort_values 一起使用:

print (pd.lreshape(df, {'c': ['c1', 'c2', 'c3'], 'p': ['p1', 'p2', 'p3']})
         .sort_values('cust'))

   cust   c   p
0     1   2   3
2     1   4   5
4     1   6   7
1     8   9  10
3     8  11  12
5     8  13  14

关于python - 在python中转置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38093334/

相关文章:

python - 从 scapy 数据包中获取信息字符串

python-2.7 - 编译错误: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

python - Pycharm 无法识别内置函数

python - Python 百分位数函数

python - 不使用内置方法删除字符串中的空格

python - 如何在 Django QuerySet 中使用 Python 类型提示?

python - TensorFlow Keras 'accuracy' 引擎盖下的指标实现

python - 在Python中释放大对象

python - 如何使用python脚本将sql.gz文件导入mysql数据库?

python - 汽车跟踪级联::Typeerror 整数到 float