我想在已经存在的数据帧中添加5个新行,并通过计算其后面的项来添加每一个新行中的基数。
我有一个看起来像这样的数据帧:
df.tail()
周期 | 值2 | fieldName//tr>||
---|---|---|---|
18 | 2021 | 3.87 | eps|
19 | 2022 | 3.97 | eps[/tr>|
20 | 2023 | 4.245 | eps|
21 | 2024 | 4.565 | eps |
您可以(基本上创建一个最后一行重复n次的df,然后将[1,2,…n]添加到period
和value2
列。
n = 5
temp = pd.DataFrame([df.iloc[-1]]*n)
temp['period'] = temp['period'].add(np.arange(n+1)[1:])
temp['value2'] = temp['value2'].add(np.arange(n+1)[1:])
out = pd.concat([df, temp], ignore_index=True)
打印(输出(:
period value2 fieldName
0 2021 3.870 eps
1 2022 3.970 eps
2 2023 4.245 eps
3 2024 4.565 eps
4 2025 5.565 eps
5 2026 6.565 eps
6 2027 7.565 eps
7 2028 8.565 eps
8 2029 9.565 eps