gpt4 book ai didi

python - 将日期时间索引透视到开始列和结束列

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:40 24 4
gpt4 key购买 nike

我在切换此数据时遇到问题:

datetime               transmission #
2019-07-12 00:03:06 124
2019-07-12 00:04:56 124
2019-07-12 00:20:10 125
2019-07-12 00:21:33 125

使用 python 模块 pandas 转换为如下格式:

transmission #   start                  end
124 2019-07-12 00:03:06 2019-07-12 00:04:56
125 2019-07-12 00:20:10 2019-07-12 00:21:33

起初,我认为我可以使用索引为 Transmission # 且值为 datetime 的数据透视表来完成此操作,但我似乎无法使其工作。

print(df.pivot(index = 'ConnectDisconnect', columns=['start', 'end'], values='data_point_time'))

ConnectDisconnect 是传输#。我认为这会起作用,但它只是输出

Traceback (most recent call last):
File "data.py", line 28, in <module>
print(df.pivot(index = 'ConnectDisconnect', columns=['start', 'end'], values='data_point_time'))
File "C:\Program Files (x86)\Python37-32\lib\site-packages\pandas\core\generic.py", line 5067, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'pivot'

如果有人能帮我解决这个问题,我将不胜感激

最佳答案

df = pd.read_csv('test.csv', sep=r'\s{2,}', engine='python')
n = len(df.index)//2
x = df['datetime']
# drop the column of 'datetime'
df = df.drop('datetime', axis=1)
# Remove the duplicated row
df = df.drop_duplicates()
df.index=range(n)
start = x[0::2]
start.index=range(n)
end = x[1::2]
end.index = range(n)
df['start'] =start
df['end']=end
print(df.to_string(index=False))

输出为

transmission #                start                  end
124 2019-07-12 00:03:06 2019-07-12 00:04:56
125 2019-07-12 00:20:10 2019-07-12 00:21:33

关于python - 将日期时间索引透视到开始列和结束列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57120458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com