创建datafram索引

2024-09-29 23:25:45 发布

您现在位置:Python中文网/ 问答频道 /正文

从以下代码开始:

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

索引已在datetime中。目标是将上面的索引替换为下面的代码创建的新索引,并将所有值保留在vento列中:

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

如果有人能帮忙我会很感激的。你知道吗


Tags: 代码importnumpypandasdataindexasdir
1条回答
网友
1楼 · 发布于 2024-09-29 23:25:45

数据源:

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

相关问题 更多 >

    热门问题