我想在数据框中创建一个额外的列,而不必遍历所有步骤
In function ‘int max_of_four(int, int, int, int)’:
error: control reaches end of non-void function [-Werror=return-type]
答案 0 :(得分:0)
假设我们有一组15个整数:
df = pd.DataFrame([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], columns=['original_data'])
我们定义应该在第n
行中添加第n行,并在x
行中添加nth
行多少次
n = 5
x = 2
(
df
# Add `x` columsn which are all shifted `n` rows
.assign(**{
'n{} x{}'.format(n, x): df['original_data'].shift(n*x)
for x in range(1, reps)})
# take the rowwise sum
.sum(axis=1)
)
输出:
original_data n5 x1
0 1 NaN
1 2 NaN
2 3 NaN
3 4 NaN
4 5 NaN
5 6 1.0
6 7 2.0
7 8 3.0
8 9 4.0
9 10 5.0
10 11 6.0
11 12 7.0
12 13 8.0
13 14 9.0
14 15 10.0