Pandas DataFrame.resample(~) 方法根据时间执行 group-by。这些参数本身很难解释,因此我们建议查看我们的示例以进行澄清。
参数
1.rule | DateOffset 或 Timedelta 或 string
每组的时间长度。
2. axis | int 或 string | optional
是否对每行或列重新采样:
|
轴 |
说明 |
|---|---|
|
|
对每一行重新采样。 |
|
|
对每列重新采样。 |
默认情况下,axis=0 。
3. closed | string | optional
bin区间的哪一侧是封闭的:
|
值 |
说明 |
|---|---|
|
|
bin区间右侧封闭(含) |
|
|
bin 间隔的左侧是封闭的 |
默认情况下, closed="left" ,但以下频率偏移除外,默认情况下具有 closed="right" :
‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’
4. label | string | optional
bin 间隔的哪一侧被标记:
|
值 |
说明 |
|---|---|
|
|
bin 间隔的右侧已标记 |
|
|
bin 间隔的左侧已标记 |
默认情况下, label=None ,但以下频率偏移除外,默认情况下具有 label="right" :
‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’
5. convention | string | optional
仅当源 DataFrame 的索引为 PeriodIndex 时,这才相关。是否使用 PeriodIndex 的开始时间或结束时间:
|
值 |
说明 |
|---|---|
|
|
使用 |
|
|
使用 |
默认情况下,convention="start" 。
6. kind | None 或 string | optional
结果索引的数据类型:
|
值 |
说明 |
|---|---|
|
|
保留结果索引不变。 |
|
|
将结果索引转换为 |
|
|
将结果索引转换为 |
默认情况下,kind=None 。
7. loffset | timedelta | optional
应用在时间标签上的偏移量。默认情况下,loffset=None 。
8. base | int | optional
第一组中包含的行数。查看示例以进行澄清。默认情况下,base=0 。
9. on | string | optional
用于重新采样而不是索引的列。该列的类型必须是 datetime-like (例如 datetime64 )。默认情况下,索引将用于重采样。
10.level | string 或 int | optional
用于重采样的级别。仅当 DataFrame 具有多索引时,这才相关。 level 也必须是 datetime-like 类型(例如 datetime64 )。
返回值
Resampler 对象。
例子
考虑以下 DataFrame :
date_index = pd.date_range("2020/12/25", periods=4)
df = pd.DataFrame({"A":[2,3,4,5],"B":[6,7,8,9]}, index=date_index)
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9
这里, df 的索引是 DatetimeIndex 类型。
基本用法
计算每组连续 2 天的总和:
df.resample(rule="2D").sum() # returns a DataFrame
A B
2020-12-25 5 13
2020-12-27 9 17
默认情况下,当随后调用 sum() 等聚合函数时,会考虑所有列。要仅在某些列上应用该函数,请使用 [] 表示法,如下所示:
df.resample(rule="2D")["A"].sum() # returns a Series since we only selected 1 column
2020-12-25 5
2020-12-27 9
Freq: 2D, Name: A, dtype: int64
指定关闭
考虑与上面相同的df:
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9
默认为closed="left"(检查参数说明是否有异常),这意味着:
-
左侧 bin 间隔包含在内
-
右 bin 间隔是排他的
df.resample(rule="2D", closed="left").sum()
A B
2020-12-25 5 13 # Sum of 12-25 and 12-26
2020-12-27 9 17 # Sum of 12-27 and 12-28
另一方面,传递 closed="right" 意味着:
-
左侧 bin 间隔不包括在内
-
正确的 bin 间隔包含在内
df.resample(rule="2D", closed="right").sum()
A B
2020-12-23 2 6 # Sum of 12-24 and 12-25
2020-12-25 7 15 # Sum of 12-26 and 12-27
2020-12-27 5 9 # Sum of 12-28 and 12-29
供您参考,这里再次是我们的df:
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9
指定标签
默认为label="left"(检查参数说明是否有异常),表示使用左侧区间bin的标签:
df.resample(rule="2D", label="left").sum()
A B
2020-12-25 5 13 # Sum of 12-25 and 12-26
2020-12-27 9 17 # Sum of 12-27 and 12-28
要使用右侧间隔箱的标签,请设置label="right",如下所示:
df.resample(rule="2D", label="right").sum()
A B
2020-12-27 5 13 # Sum of 12-25 and 12-26
2020-12-29 9 17 # Sum of 12-27 and 12-28
指定种类
要获得 DatetimeIndex ,请像这样设置 kind="timestamp" :
df.resample(rule="2D", kind="timestamp").sum().index
DatetimeIndex(['2020-12-25', '2020-12-27'], dtype='datetime64[ns]', freq='2D')
要获得 PeriodIndex ,请像这样设置 kind="period" :
df.resample(rule="2D", kind="period").sum().index
PeriodIndex(['2020-12-25', '2020-12-27'], dtype='period[2D]', freq='2D')
指定loffset
供您参考,这里再次是我们的df:
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9
要将标签移动 1 天,请传递 loffset="1D",如下所示:
df.resample(rule="2D", loffset="1D").sum()
A B
2020-12-26 5 13
2020-12-28 9 17
请注意,只有标签发生了偏移 - DataFrame 的结果值不受影响。
作为对比,我们不指定loffset的情况如下:
df.resample(rule="2D").sum()
A B
2020-12-25 5 13
2020-12-27 9 17
指定基数
考虑以下 DataFrame :
date_index = pd.date_range("2020/12/25", periods=4)
df = pd.DataFrame({"A":[2,3,4,5],"B":[6,7,8,9]}, index=date_index)
df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-12-28 5 9
以 4 天产量重新采样:
df.resample(rule="4D").sum()
A B
2020-12-25 14 30
指定base 可确保第一组具有base 行数。例如,base=2 产生:
df.resample(rule="4D", base=2).sum()
A B
2020-12-23 5 13
2020-12-27 9 17
在这里,请注意我们如何从 12-23 而不是 12-25 开始。这是因为 base=2 确保仅 df 的前两行放置在第一组中。由于我们仍然按连续 4 天进行分组,因此将开始日期更改为 12-23 。
指定于
默认,resample(~)方法假设 index DataFrame 的值为datetime-like。参数on允许您重新采样柱子.
考虑以下 DataFrame :
date_index = pd.date_range("2020/12/25", periods=4)
df = pd.DataFrame({"A":date_index,"B":[3,4,5,6]})
df
A B
0 2020-12-25 3
1 2020-12-26 4
2 2020-12-27 5
3 2020-12-28 6
要对列 A 重新采样:
df.resample(rule="2D", on="A").sum()
B
A
2020-12-25 7
2020-12-27 11
请注意以下事项:
-
A列成为新索引。 -
分配给这个新索引的名称是列标签(在本例中为
A)。
相关用法
- Python Pandas DataFrame reset_index方法用法及代码示例
- Python PySpark DataFrame repartition方法用法及代码示例
- Python PySpark DataFrame replace方法用法及代码示例
- Python Pandas DataFrame reorder_levels方法用法及代码示例
- Python Pandas DataFrame reindex方法用法及代码示例
- Python Pandas DataFrame replace方法用法及代码示例
- Python Pandas DataFrame rename_axis方法用法及代码示例
- Python Pandas DataFrame rename方法用法及代码示例
- Python Pandas DataFrame rank方法用法及代码示例
- Python Pandas DataFrame rdiv方法用法及代码示例
- Python Pandas DataFrame radd方法用法及代码示例
- Python PySpark DataFrame rdd属性用法及代码示例
- Python Pandas DataFrame rsub方法用法及代码示例
- Python Pandas DataFrame round方法用法及代码示例
- Python PySpark DataFrame randomSplit方法用法及代码示例
- Python Pandas DataFrame rolling方法用法及代码示例
- Python Pandas DataFrame rpow方法用法及代码示例
- Python Pandas DataFrame rfloordiv方法用法及代码示例
- Python Pandas DataFrame rtruediv方法用法及代码示例
- Python Pandas DataFrame rmod方法用法及代码示例
- Python Pandas DataFrame rmul方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | resample method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
