首页 > 解决方案 > 根据动态列数将列添加到数据框的末尾

问题描述

我有一个数据框,其中列数逐月变化。我想在每个数据框的末尾创建 3 个空列。

我使用insert,但由于列数逐月变化,我留下了错误

IndexError: index 59 is out of bounds for axis 0 with size 58

我的代码:

netnewprocess.insert(59, 'date', '')
netnewprocess.insert(60, 'Analyst Review', '')
netnewprocess.insert(61, 'SM Review', '')

标签: pythonpandas

解决方案


如果您只是像这样添加一列,它将将该列附加到 DataFrame 的右侧,并且您不需要指定索引:

netnewprocess['date'] = ''
netnewprocess['Analyst Review'] = ''
netnewprocess['SM Review'] = ''

推荐阅读