![](/img/trans.png)
[英]How do I add a piece of string from one df column to another in python/pandas?
[英]How to add column one after another in pandas df using for loop
我正在使用for循环。 我将在循环内进行一些操作。 The result would be an numpy array
。 现在我只想将numpy数组存储在每个for循环值的pandas df中。
我的df如下图所示
Lt Lg
-7.0 -7.0
-7.0 -6.9
-7.0 -6.8
. .
. .
. .
. .
6.9 6.8
6.9 6.9
df.shape is (19600,2)
代码示例
for i in range(21,25):
' do some operations and return below numpy array of length 19600'
result_array = [0,0,0.2......0.8,0.6] # lenght of array = 19600
df[i] = result_array
结果我得到了
Lt Lg 24
-7.0 -7.0 0
-7.0 -6.9 0.2
-7.0 -6.8 0
. . .
. . .
. . .
. . .
6.9 6.8 0.8
6.9 6.9 1
需求
Lt Lg 21 22 23 24
-7.0 -7.0 0 0 0 0
-7.0 -6.9 0.2 0 0.4 0.2
-7.0 -6.8 0 0.2 0.6 0
. . . . . .
. . . . . .
. . . . . .
. . . . . .
6.9 6.8 0.8 0 0.6 0.8
6.9 6.9 1 0.4 0.2 1
它一次又一次地覆盖该列,并生成与df合并的最后一个计算的result_array
。 但是我需要将每个result_array
与df结合/合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.