【问题标题】:How to reshape the date using values of a column as column names in pandas?如何使用列的值作为熊猫中的列名来重塑日期?
【发布时间】:2020-09-05 08:49:46
【问题描述】:

我有这些数据:

        index       2020-08-26 16:20:00  2020-08-26 19:09:00    2020-08-26 20:04:00
0       start       230                  104                    64
1       stop        228                  185                    170
2       avail       None                 None                   None
3       Status      False                False                  False

我想把它转换成这种形式:

    index                start   stop  avail  Status
0   2020-08-26 16:20:00  230     228   None     False
1   2020-08-26 19:09:00  104     185   None     False
2   2020-08-26 20:04:00  64      170   None     False

我已经申请了

df.T

但我得到的输出是:

                    0       1       2        3
index               start   stop    endtime  status
2020-08-26 16:20:00 230     228     None     False
2020-08-26 19:09:00 104     185     None     False
2020-08-26 20:04:00 64      170     None     False

我想要索引、开始、停止、结束时间、状态作为列名

【问题讨论】:

  • 尝试转置df.T。您可能首先需要将“索引”列作为您的 df df.index = df['index'] 的索引
  • @Anoop Sharma 知道了,谢谢
  • 这能回答你的问题吗? how to switch columns rows in a pandas dataframe
  • @das-g 没看到我得到了什么。
  • @PriyankaS.Desai 检查df = df.set_index('index').T

标签: python python-3.x pandas python-2.7 python-requests


【解决方案1】:

您没有提供过渡代码,但在完成后您可以重命名列:

df.rename(columns={"0": "start", "1": "stop", "2": "end time", "3": "status"})

然后删除第一行:

df.drop(df.index[:1], inplace=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 2020-08-19
    • 2013-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    相关资源
    最近更新 更多