Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python.dataframe resample.last如何确保数据来自同一行_Python_Pandas_Dataframe - Fatal编程技术网

python.dataframe resample.last如何确保数据来自同一行

python.dataframe resample.last如何确保数据来自同一行,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个像这样的数据框 df = pd.DataFrame({'col1':range(9), 'col2': list(range(6)) + [np.nan] *3}, index = pd.date_range('1/1/2000', periods=9, freq='T')) df Out[63]: col1 col2 2000-01-01 00:00:00 0 0.0 2000-01-01 00:01:00

我有一个像这样的数据框

df = pd.DataFrame({'col1':range(9), 'col2': list(range(6)) + [np.nan] *3}, 
    index = pd.date_range('1/1/2000', periods=9, freq='T'))

df
Out[63]: 
                     col1  col2
2000-01-01 00:00:00     0   0.0
2000-01-01 00:01:00     1   1.0
2000-01-01 00:02:00     2   2.0
2000-01-01 00:03:00     3   3.0
2000-01-01 00:04:00     4   4.0
2000-01-01 00:05:00     5   5.0
2000-01-01 00:06:00     6   NaN
2000-01-01 00:07:00     7   NaN
2000-01-01 00:08:00     8   NaN
当我最后一次用方法进行重采样时

如上所述,第6分钟行中有col1上的数据,因此重采样后,col1将用第6分钟行中的数据填充,但col2将用第5分钟行填充,是否有方法确保重采样后的两个数据都来自第6分钟行,这意味着如果col1有数据,重采样将不会用last填充col2的NaN,但就让它保持原样

Out[60]: 
                     col1  col2
2000-01-01 00:00:00     0   0.0
2000-01-01 00:03:00     3   3.0
2000-01-01 00:06:00     6   NaN  <--- if there at least one col has data,the whole row will be used in resample
2000-01-01 00:09:00     8   NaN
这就是pandas中last的工作方式,它将返回组的last notnull值,如果您想获得包含nan的最后一个值,请使用apply与iloc进行检查

这就是pandas中last的工作方式,它将返回组的last notnull值,如果您想获得包含nan的最后一个值,请使用apply与iloc进行检查

也可以使用.nth-1或.tail1使用ceil来形成相同的组:

df.groupby(df.index.ceil('3T')).nth(-1)
                     col1  col2
2000-01-01 00:00:00     0   0.0
2000-01-01 00:03:00     3   3.0
2000-01-01 00:06:00     6   NaN
2000-01-01 00:09:00     8   NaN
也可以使用.nth-1或.tail1使用ceil来形成相同的组:

df.groupby(df.index.ceil('3T')).nth(-1)
                     col1  col2
2000-01-01 00:00:00     0   0.0
2000-01-01 00:03:00     3   3.0
2000-01-01 00:06:00     6   NaN
2000-01-01 00:09:00     8   NaN
相关的:相关的:
df.groupby(df.index.ceil('3T')).nth(-1)
                     col1  col2
2000-01-01 00:00:00     0   0.0
2000-01-01 00:03:00     3   3.0
2000-01-01 00:06:00     6   NaN
2000-01-01 00:09:00     8   NaN