How to add values to a new column after n rows?
up vote
0
down vote
favorite
I have this loop that continuously checks api data and add it to an array.
Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.
import numpy as np
list_b =
i = 0
while True:
i += 1
list_a =
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if len(array) > 10:
x = indicator_function(array[:,0])
np.append(x) # in a 4th column
if i == 15:
break
print(array.shape)
print(len(array))
print(array)
The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.
The array should be formed like this:
1 33 44
2 33 44
...
9 33 44
10 33 44
11 33 44 x
12 33 44 x
numpy
add a comment |
up vote
0
down vote
favorite
I have this loop that continuously checks api data and add it to an array.
Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.
import numpy as np
list_b =
i = 0
while True:
i += 1
list_a =
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if len(array) > 10:
x = indicator_function(array[:,0])
np.append(x) # in a 4th column
if i == 15:
break
print(array.shape)
print(len(array))
print(array)
The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.
The array should be formed like this:
1 33 44
2 33 44
...
9 33 44
10 33 44
11 33 44 x
12 33 44 x
numpy
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this loop that continuously checks api data and add it to an array.
Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.
import numpy as np
list_b =
i = 0
while True:
i += 1
list_a =
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if len(array) > 10:
x = indicator_function(array[:,0])
np.append(x) # in a 4th column
if i == 15:
break
print(array.shape)
print(len(array))
print(array)
The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.
The array should be formed like this:
1 33 44
2 33 44
...
9 33 44
10 33 44
11 33 44 x
12 33 44 x
numpy
I have this loop that continuously checks api data and add it to an array.
Then I calculate an indicator based on one column of the array that I want to add the the array. The issue only is that the indicator need 10 data point to get going.
import numpy as np
list_b =
i = 0
while True:
i += 1
list_a =
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if len(array) > 10:
x = indicator_function(array[:,0])
np.append(x) # in a 4th column
if i == 15:
break
print(array.shape)
print(len(array))
print(array)
The issue is that I cant append to the array because it doesnt have the same dimensions as the array before. So how can I extend the array by one column or insert the new indicators values in a new column after 10 rows.
The array should be formed like this:
1 33 44
2 33 44
...
9 33 44
10 33 44
11 33 44 x
12 33 44 x
numpy
numpy
asked Nov 11 at 2:54
Sungod3k
34
34
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if
statement up above list_b.append(list_a)
and change it to
if (i > 10):
x = indicator_function(array[:,0])
list_a.append(x)
else:
list_a.append(None)
add a comment |
up vote
0
down vote
if i < 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(55)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if i > 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(66)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
@sahil yes that works.
add a comment |
up vote
0
down vote
There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:
import itertools
import numpy.ma as ma
array = None
list_b =
def indicator_function(arr):
return -1
for i in itertools.count():
list_a =
list_b.append(list_a)
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(np.nan)
array = ma.MaskedArray(list_b)
# assign mask or indicator_function, as appropriate
ix = 10 + 1
array[:ix, -1] = ma.masked
array[ix:, -1] = indicator_function(array[:,0])
if i == 15:
break
This results in a masked array
that looks like:
[[0.0, 33.0, 44.0, --],
[1.0, 33.0, 44.0, --],
[2.0, 33.0, 44.0, --],
[3.0, 33.0, 44.0, --],
[4.0, 33.0, 44.0, --],
[5.0, 33.0, 44.0, --],
[6.0, 33.0, 44.0, --],
[7.0, 33.0, 44.0, --],
[8.0, 33.0, 44.0, --],
[9.0, 33.0, 44.0, --],
[10.0, 33.0, 44.0, --],
[11.0, 33.0, 44.0, -1.0],
[12.0, 33.0, 44.0, -1.0],
[13.0, 33.0, 44.0, -1.0],
[14.0, 33.0, 44.0, -1.0],
[15.0, 33.0, 44.0, -1.0]]
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if
statement up above list_b.append(list_a)
and change it to
if (i > 10):
x = indicator_function(array[:,0])
list_a.append(x)
else:
list_a.append(None)
add a comment |
up vote
0
down vote
From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if
statement up above list_b.append(list_a)
and change it to
if (i > 10):
x = indicator_function(array[:,0])
list_a.append(x)
else:
list_a.append(None)
add a comment |
up vote
0
down vote
up vote
0
down vote
From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if
statement up above list_b.append(list_a)
and change it to
if (i > 10):
x = indicator_function(array[:,0])
list_a.append(x)
else:
list_a.append(None)
From what I understand, numpy doesn't allow for jagged arrays (arrays with a changing number of columns). You might have to create an entirely new array. Or, if you want to do something crazy, you could move the if
statement up above list_b.append(list_a)
and change it to
if (i > 10):
x = indicator_function(array[:,0])
list_a.append(x)
else:
list_a.append(None)
answered Nov 11 at 3:24
Sahil Makhijani
538
538
add a comment |
add a comment |
up vote
0
down vote
if i < 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(55)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if i > 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(66)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
@sahil yes that works.
add a comment |
up vote
0
down vote
if i < 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(55)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if i > 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(66)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
@sahil yes that works.
add a comment |
up vote
0
down vote
up vote
0
down vote
if i < 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(55)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if i > 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(66)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
@sahil yes that works.
if i < 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(55)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
if i > 10:
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(66)
list_b.append(list_a)
array = np.array(list_b, dtype=float)
@sahil yes that works.
answered Nov 11 at 3:41
Sungod3k
34
34
add a comment |
add a comment |
up vote
0
down vote
There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:
import itertools
import numpy.ma as ma
array = None
list_b =
def indicator_function(arr):
return -1
for i in itertools.count():
list_a =
list_b.append(list_a)
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(np.nan)
array = ma.MaskedArray(list_b)
# assign mask or indicator_function, as appropriate
ix = 10 + 1
array[:ix, -1] = ma.masked
array[ix:, -1] = indicator_function(array[:,0])
if i == 15:
break
This results in a masked array
that looks like:
[[0.0, 33.0, 44.0, --],
[1.0, 33.0, 44.0, --],
[2.0, 33.0, 44.0, --],
[3.0, 33.0, 44.0, --],
[4.0, 33.0, 44.0, --],
[5.0, 33.0, 44.0, --],
[6.0, 33.0, 44.0, --],
[7.0, 33.0, 44.0, --],
[8.0, 33.0, 44.0, --],
[9.0, 33.0, 44.0, --],
[10.0, 33.0, 44.0, --],
[11.0, 33.0, 44.0, -1.0],
[12.0, 33.0, 44.0, -1.0],
[13.0, 33.0, 44.0, -1.0],
[14.0, 33.0, 44.0, -1.0],
[15.0, 33.0, 44.0, -1.0]]
add a comment |
up vote
0
down vote
There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:
import itertools
import numpy.ma as ma
array = None
list_b =
def indicator_function(arr):
return -1
for i in itertools.count():
list_a =
list_b.append(list_a)
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(np.nan)
array = ma.MaskedArray(list_b)
# assign mask or indicator_function, as appropriate
ix = 10 + 1
array[:ix, -1] = ma.masked
array[ix:, -1] = indicator_function(array[:,0])
if i == 15:
break
This results in a masked array
that looks like:
[[0.0, 33.0, 44.0, --],
[1.0, 33.0, 44.0, --],
[2.0, 33.0, 44.0, --],
[3.0, 33.0, 44.0, --],
[4.0, 33.0, 44.0, --],
[5.0, 33.0, 44.0, --],
[6.0, 33.0, 44.0, --],
[7.0, 33.0, 44.0, --],
[8.0, 33.0, 44.0, --],
[9.0, 33.0, 44.0, --],
[10.0, 33.0, 44.0, --],
[11.0, 33.0, 44.0, -1.0],
[12.0, 33.0, 44.0, -1.0],
[13.0, 33.0, 44.0, -1.0],
[14.0, 33.0, 44.0, -1.0],
[15.0, 33.0, 44.0, -1.0]]
add a comment |
up vote
0
down vote
up vote
0
down vote
There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:
import itertools
import numpy.ma as ma
array = None
list_b =
def indicator_function(arr):
return -1
for i in itertools.count():
list_a =
list_b.append(list_a)
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(np.nan)
array = ma.MaskedArray(list_b)
# assign mask or indicator_function, as appropriate
ix = 10 + 1
array[:ix, -1] = ma.masked
array[ix:, -1] = indicator_function(array[:,0])
if i == 15:
break
This results in a masked array
that looks like:
[[0.0, 33.0, 44.0, --],
[1.0, 33.0, 44.0, --],
[2.0, 33.0, 44.0, --],
[3.0, 33.0, 44.0, --],
[4.0, 33.0, 44.0, --],
[5.0, 33.0, 44.0, --],
[6.0, 33.0, 44.0, --],
[7.0, 33.0, 44.0, --],
[8.0, 33.0, 44.0, --],
[9.0, 33.0, 44.0, --],
[10.0, 33.0, 44.0, --],
[11.0, 33.0, 44.0, -1.0],
[12.0, 33.0, 44.0, -1.0],
[13.0, 33.0, 44.0, -1.0],
[14.0, 33.0, 44.0, -1.0],
[15.0, 33.0, 44.0, -1.0]]
There's no builtin support for proper jagged arrays in numpy. The best bet for your application is probably a masked array:
import itertools
import numpy.ma as ma
array = None
list_b =
def indicator_function(arr):
return -1
for i in itertools.count():
list_a =
list_b.append(list_a)
list_a.append(i)
list_a.append(33)
list_a.append(44)
list_a.append(np.nan)
array = ma.MaskedArray(list_b)
# assign mask or indicator_function, as appropriate
ix = 10 + 1
array[:ix, -1] = ma.masked
array[ix:, -1] = indicator_function(array[:,0])
if i == 15:
break
This results in a masked array
that looks like:
[[0.0, 33.0, 44.0, --],
[1.0, 33.0, 44.0, --],
[2.0, 33.0, 44.0, --],
[3.0, 33.0, 44.0, --],
[4.0, 33.0, 44.0, --],
[5.0, 33.0, 44.0, --],
[6.0, 33.0, 44.0, --],
[7.0, 33.0, 44.0, --],
[8.0, 33.0, 44.0, --],
[9.0, 33.0, 44.0, --],
[10.0, 33.0, 44.0, --],
[11.0, 33.0, 44.0, -1.0],
[12.0, 33.0, 44.0, -1.0],
[13.0, 33.0, 44.0, -1.0],
[14.0, 33.0, 44.0, -1.0],
[15.0, 33.0, 44.0, -1.0]]
answered Nov 11 at 3:48
tel
3,4911427
3,4911427
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245450%2fhow-to-add-values-to-a-new-column-after-n-rows%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown