Pandas DataFrame缺失日期填充、Wells列列表追加及前向填充需求
Pandas DataFrame 日期填充与列表累加处理
需求
- 填充指定日期范围内的缺失月度起始日期
- 对
vol列执行前向填充(ffill) - 按日期顺序累加
Wells列的列表元素,形成连续的集合列表 - 调整
count和Sum_Cumm列以匹配目标格式:原始行保留原count值,填充行count设为1;Sum_Cumm按日期累计更新
原始DataFrame
import pandas as pd data = { 'StartDate': ['1967-10-01', '1968-01-01', '1968-03-01', '1968-10-01'], 'Wells': [['MUN-523', 'MUN-354', 'MUN-2660'], ['MUN-152'], ['MUN-1032'], ['MUN-16128']], 'count': [50, 1, 1, 1], 'Sum_Cumm': [50, 51, 52, 53], 'vol': [8503.323620, 8336.591784, 8176.272712, 9191.110200] } df = pd.DataFrame(data) df['StartDate'] = pd.to_datetime(df['StartDate'])
展示原始数据:
StartDate Wells count Sum_Cumm vol 0 1967-10-01 [MUN-523, MUN-354, MUN-2660] 50 50 8503.323620 1 1968-01-01 [MUN-152] 1 51 8336.591784 2 1968-03-01 [MUN-1032] 1 52 8176.272712 3 1968-10-01 [MUN-16128] 1 53 9191.110200
当前代码
newdf = (newdf.set_index('StartDate').reindex(pd.date_range('10-01-1967', '12-31-1994', freq='MS')).rename_axis(['StartDate']).reset_index()).ffill(newdf['vol'])
期望目标DataFrame
StartDate Wells count Sum_Cumm vol 0 1967-10-01 [MUN-523, MUN-354, MUN-2660] 50 50 8503.323620 1 1967-11-01 [MUN-523, MUN-354, MUN-2660] 1 51 8503.323620 2 1967-12-01 [MUN-523, MUN-354, MUN-2660] 1 51 8503.323620 3 1968-01-01 [MUN-523, MUN-354, MUN-2660, MUN-152] 1 52 8336.591784 4 1968-02-01 [MUN-523, MUN-354, MUN-2660, MUN-152] 1 53 8336.591784 5 1968-03-01 [MUN-523, MUN-354, MUN-2660, MUN-152, MUN-1032] 1 53 8176.272712 6 1968-04-01 [MUN-523, MUN-354, MUN-2660, MUN-152, MUN-1032] 1 53 8176.272712
完整解决方案
当前代码仅部分处理了vol列,未覆盖Wells累加及count、Sum_Cumm的逻辑,以下是完整实现代码:
import pandas as pd # 加载并预处理原始数据 data = { 'StartDate': ['1967-10-01', '1968-01-01', '1968-03-01', '1968-10-01'], 'Wells': [['MUN-523', 'MUN-354', 'MUN-2660'], ['MUN-152'], ['MUN-1032'], ['MUN-16128']], 'count': [50, 1, 1, 1], 'Sum_Cumm': [50, 51, 52, 53], 'vol': [8503.323620, 8336.591784, 8176.272712, 9191.110200] } df = pd.DataFrame(data) df['StartDate'] = pd.to_datetime(df['StartDate']) # 1. 填充缺失的月度日期 date_range = pd.date_range(start='1967-10-01', end='1994-12-31', freq='MS') newdf = df.set_index('StartDate').reindex(date_range).rename_axis('StartDate').reset_index() # 2. 前向填充vol列 newdf['vol'] = newdf['vol'].ffill() # 3. 累加Wells列的列表元素 newdf['is_original'] = newdf['Wells'].notna() newdf['Wells'] = newdf['Wells'].ffill() # 生成累计列表 newdf['cum_wells'] = newdf['Wells'] for i in range(1, len(newdf)): if newdf.loc[i, 'is_original']: newdf.loc[i, 'cum_wells'] = newdf.loc[i-1, 'cum_wells'] + newdf.loc[i, 'Wells'] else: newdf.loc[i, 'cum_wells'] = newdf.loc[i-1, 'cum_wells'] newdf['Wells'] = newdf.pop('cum_wells') # 4. 调整count和Sum_Cumm列 newdf['count'] = newdf['count'].fillna(1) initial_sum = newdf.loc[newdf['is_original'].idxmax(), 'Sum_Cumm'] newdf['Sum_Cumm'] = initial_sum + newdf['count'].cumsum() - newdf.loc[newdf['is_original'].idxmax(), 'count'] # 清理临时列 newdf.drop('is_original', axis=1, inplace=True) # 查看前7行结果 print(newdf.head(7))
运行代码后即可得到与期望一致的DataFrame。
内容的提问来源于stack exchange,提问作者user11958450
相关产品推荐
相关产品推荐

