我有三个pandas数据框,其中包含三种类型的索引15分钟,1分15秒,我将NaN
添加到数据框中,并在下图中绘制主题。
图表:
现在我要替换其中一个数据框NaN
,我使用ffill()
,它有效,但我需要限制填充NaN
,我不需要我标记为红色的内容。
Graph2:
我的情节应该是这样的:
Dataframes:
http://bayanbox.ir/id/1324113030042053806?download
http://bayanbox.ir/id/774076250887409862?download
http://bayanbox.ir/id/6217190851751601245?download
来源:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# 1 minutes recorded data
data = pd.read_csv('1m.csv', parse_dates=True, index_col='time')
# 15 minutes recorded data
data2 = pd.read_csv('15m.csv', parse_dates=True, index_col='time')
# 15 seconds recorded data
data3 = pd.read_csv('15s.csv', parse_dates=True, index_col='time')
del data['Unnamed: 0'], data2['Unnamed: 0'], data3['Unnamed: 0']
def add_nan(DF, T):
start = DF.time[len(DF)-1]
stop = DF.time[0]
rng = pd.date_range(start, stop, freq=T)
DF = DF.drop_duplicates('time').set_index('time').reindex(rng)
return DF
data = pd.DataFrame({"1-min":np.array(data.Height[:]), "time":data.index})
data2 = pd.DataFrame({"15-min":np.array(data2.Height[:]), "time":data2.index})
data3 = pd.DataFrame({"15-sec":np.array(data3.Height[:]), "time":data3.index})
data = add_nan(data, '1min')
data2 = add_nan(data2, '15min')
data3 = add_nan(data3, '1S')
ax = data.plot(color='g', figsize=(10, 6))
data2.plot(ax=ax, color='b')
data3.plot(ax=ax, style='.-r')
plt.savefig('plot.png')
答案 0 :(得分:0)
根据Pandas documentation,限制参数应设置为True
DataFrame.ffill(axis=0, inplace=False, limit=None, downcast=None)
Synonym for NDFrame.fillna(method=’ffill’)
这是向数据框添加NaN
数据的功能,因为在数据框中应设置限制NaN
填充limit=True
:
def add_nan(DF, T):
start = DF.time[len(DF)-1]
stop = DF.time[0]
rng = pd.date_range(start, stop, freq=T)
DF = DF.drop_duplicates('time').set_index('time').reindex(rng)
return DF.ffill(limit=True)