【问题标题】:Pandas Python DataFrame format conversion [duplicate]Pandas Python DataFrame格式转换[重复]
【发布时间】:2022-01-22 21:30:13
【问题描述】:

1 Pandas DF 有这种格式,但我想转换成另一种格式,即图像 2

请提供有关我如何转换的任何想法。

【问题讨论】:

标签: python pandas numpy


【解决方案1】:

使用pd.melt:

out = df.melt(['DateTime', 'Machine'], var_name='Variable', value_name='Value') \
        .sort_values('DateTime', ascending=False).reset_index(drop=True)
print(out)

# Output:
              DateTime Machine     Variable  Value
0  2021-02-01 09:26:00      GE  Temperature     30
1  2021-02-01 09:26:00      GE        Speed   1200
2  2021-02-01 09:25:00      GE  Temperature     32
3  2021-02-01 09:25:00      GE        Speed   1200
4  2021-02-01 09:24:00      GE  Temperature     35
5  2021-02-01 09:24:00      GE        Speed   1200
6  2021-02-01 09:23:00      GE  Temperature     26
7  2021-02-01 09:23:00      GE        Speed   1200
8  2021-02-01 09:22:00      GE  Temperature     31
9  2021-02-01 09:22:00      GE        Speed   1200
10 2021-02-01 09:21:00      GE  Temperature     32
11 2021-02-01 09:21:00      GE        Speed   1200
12 2021-02-01 09:20:00      GE  Temperature     35
13 2021-02-01 09:20:00      GE        Speed   1200
14 2021-02-01 09:19:00      GE  Temperature     38
15 2021-02-01 09:19:00      GE        Speed   1200

【讨论】:

  • 嗨,它解决了这个DF的问题。但对于另一个 DF,它显示错误:ValueError: Data must be 1-dimensional
  • 您必须更新您的 Pandas 版本。你的版本是什么?
  • 或者你有一列包含数组而不是标量值。
  • 我的意思是不在列名中。我认为您有一个包含数组的列值。
  • 是的,伙计……它现在可以工作了……非常感谢
猜你喜欢
  • 2017-01-08
  • 2021-12-29
  • 2017-06-09
  • 2017-10-26
  • 2020-05-20
  • 2016-08-11
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
相关资源
最近更新 更多