我在切换此数据时遇到问题:
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
起初,我以为我可以使用索引为传输#且值为datetime
的数据透视表来执行此操作,但是我似乎无法使其工作。
print(df.pivot(index = 'ConnectDisconnect', columns=['start', 'end'], values='data_point_time'))
ConnectDisconnect是transmission#
。我以为这样可以,但是只能输出
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'
如果有人可以帮助我,将不胜感激
答案 0 :(得分:0)
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