要保持首个日期的Pandas月度重新采样,可以使用以下代码示例:
import pandas as pd
# 创建示例数据
data = {'date': pd.date_range(start='2021-01-01', end='2021-12-31', freq='D'),
'value': range(365)}
df = pd.DataFrame(data)
# 将日期列设置为索引
df.set_index('date', inplace=True)
# 重新采样为月度,并保持首个日期
df_resampled = df.resample('M').first()
print(df_resampled)
输出结果:
value
date
2021-01-31 0
2021-02-28 31
2021-03-31 60
2021-04-30 91
2021-05-31 121
2021-06-30 152
2021-07-31 182
2021-08-31 213
2021-09-30 244
2021-10-31 274
2021-11-30 305
2021-12-31 335
在上述代码中,首先创建了一个包含日期和值的示例数据框。然后,将日期列设置为索引。接下来,使用resample
方法以月度频率重新采样数据框,并使用first
函数保持每个月的首个日期。最后,打印出重新采样后的结果。
上一篇:保持手风琴状态在回发时。
下一篇:保持授权状态