Leonardo Ferreira
Leonardo Ferreira

Reputation: 384

Creating index to dataframe

Starting with this code:

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')

The dataframe is something like this:

    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

The index is already in datetime. The objective is to substitute the index above to a new index created by this code below and keep all values in vento columns:

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')

Getting this index like:

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

Desired output:

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

Would be thankful if someone could help.

Upvotes: 0

Views: 104

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210972

Source 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

Solution:

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

Upvotes: 2

Related Questions