我有以下df:
Mid South North
Elec Elec Elec
t
0 0 0 0
1 43102.490062 43102.490062 43102.490062
2 41692.002871 41692.002871 41692.002871
3 40592.822117 40592.822117 40592.822117
...我想把它转换成下面的df
Mid South North
Elec Elec Elec
t
0 43102.490062 43102.490062 43102.490062
1 41692.002871 41692.002871 41692.002871
2 40592.822117 40592.822117 40592.822117
...基本上,我需要将数据移到上面的1行。
例如:如果我有索引(0-3),我应该有索引(0-2)。
发布于 2018-12-19 16:36:10
使用DataFrame.shift()方法移动索引:
import pandas as pd
# Recreate your example dataset
df = pd.DataFrame(data={'t': [0, 1, 2, 3], 'Mid Elec': [0., 43102.5, 41692.0, 40592.8], 'South Elec': [0., 43102.5, 41692.0, 40592.8], 'North Elec': [0., 43102.5, 41692.0, 40592.8]})
df.set_index('t', inplace=True)
df = df.shift(-1)输出:
Mid Elec South Elec North Elec
t
0 43102.5 43102.5 43102.5
1 41692.0 41692.0 41692.0
2 40592.8 40592.8 40592.8
3 NaN NaN NaN要删除最后一行,只需使用标准Python列表索引:
df = df[:-1]输出:
Mid Elec South Elec North Elec
t
0 43102.5 43102.5 43102.5
1 41692.0 41692.0 41692.0
2 40592.8 40592.8 40592.8https://stackoverflow.com/questions/53855385
复制相似问题