2017-05-08 123 views
0

与此代码开始:创建索引数据帧

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

vento=pd.read_csv('dados_tpm.txt') 
vento.rename(columns={'Dia_Mes_Ano_Hora_Minuto': 'Data'}) 
vento.set_index('Data') 

数据帧是这样的:

Data    Vel Dir 
    2016-07-12 16:26:00 2.4 21.0 
    2016-07-12 16:27:00 1.7 17.8 
    2016-07-12 16:29:00 14.3 14.9 

该指数已在日期时间。目的是替代指数上方通过下面这段代码创建了一个新的指数,并保持所有的值在文托列:

vento3 = pd.DataFrame({'Data':pd.date_range(start='2016-07-12 16:17:00',end='2017-04-30 22:34:00',freq='1Min')}) 
vento3.set_index('Data') 

得到这个指标,如:

Data 
2016-07-12 16:26:00 
2016-07-12 16:27:00 
2016-07-12 16:28:00 
2016-07-12 16:29:00 

所需的输出:

Data    Vel Dir 
2016-07-12 16:26:00 2.4 21.0 
2016-07-12 16:27:00 1.7 17.8 
2016-07-12 16:28:00 NaN NaN 
2016-07-12 16:29:00 14.3 14.9 

如果有人可以帮忙,会很感激。

+1

你应该能够只是做'vento.reindex(pd.date_range(开始='2016年7月12日16:17: 00',end ='2017-04-30 22:34:00',freq ='1Min'))' – EdChum

+0

如果我使用这个,我得到这个错误:ValueError:不能从重复轴重新索引 –

+0

我得到相同的错误。我不知道这是为什么。 – Moondra

回答

2

来源DF:

In [23]: df 
Out[23]: 
         Vel Dir 
Data 
2016-07-12 16:26:00 2.4 21.0 
2016-07-12 16:27:00 1.7 17.8 
2016-07-12 16:29:00 14.3 14.9 

解决方案:

In [22]: df.resample('T').mean().reset_index() 
Out[22]: 
       Data Vel Dir 
0 2016-07-12 16:26:00 2.4 21.0 
1 2016-07-12 16:27:00 1.7 17.8 
2 2016-07-12 16:28:00 NaN NaN 
3 2016-07-12 16:29:00 14.3 14.9 
+0

我不知道我可以使用重采样功能重置索引,非常感谢。 –

+0

@LeonardoFerreira,很高兴我能帮忙:-) – MaxU