I am new with array in python, please help me.
I have a multidimensional numpy array like this:
array([[ 0., 2073., 2352., 1119., 2074., 1344., 4035., 1980., 2213.,
2363., 2655., 2322., 1148., 2046., 2234., 1076., 1647., 2957.,
1968., 2246., 1723.],
[1517., 0., 891., 1537., 1993., 2231., 2574., 689., 1561.,
2157., 1517., 3275., 1566., 757., 774., 2190., 822., 1355.,
2152., 1575., 1064.],
[1597., 1329., 0., 1617., 1106., 1345., 1951., 1551., 1938.,
1270., 629., 2320., 1646., 1619., 862., 2267., 1357., 934.,
1264., 687., 342.]])
I want to add 0 at the beginning in every array, and at the end i want to add 22 of 0 of array so it becomes like this:
array([[0., 0., 2073., 2352., 1119., 2074., 1344., 4035., 1980., 2213.,
2363., 2655., 2322., 1148., 2046., 2234., 1076., 1647., 2957.,
1968., 2246., 1723.],
[0.,1517., 0., 891., 1537., 1993., 2231., 2574., 689., 1561.,
2157., 1517., 3275., 1566., 757., 774., 2190., 822., 1355.,
2152., 1575., 1064.],
[0.,1597., 1329., 0., 1617., 1106., 1345., 1951., 1551., 1938.,
1270., 629., 2320., 1646., 1619., 862., 2267., 1357., 934.,
1264., 687., 342.],
[0.,0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0, 0., 0, 0., 0.,
0., 0., 0.]])
please help me to code this
2 Answers 2
If the array is arr then you can use:
np.pad(arr, ((0, 1), (1, 0)))
answered Jan 14, 2022 at 3:28
bb1
7,9332 gold badges11 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
RamaH
thanks for your answer, it works! sorry i wanna ask an out of topic question abouot the array above, do u know how to change the position of the array? i want to switch the arr[0] with arr[2] thanks
bb1
Do you mean you want to interchange these two rows? If so, then
arr[[0, 2]] = arr[[2, 0]] should do it.You can insert 0 at the beginning of every array and then append a list of 22 0.
import numpy as np
data = np.array([[0., 2073., 2352., 1119., 2074., 1344., 4035., 1980., 2213.,
2363., 2655., 2322., 1148., 2046., 2234., 1076., 1647.,
2957.,
1968., 2246., 1723.],
[1517., 0., 891., 1537., 1993., 2231., 2574., 689., 1561.,
2157., 1517., 3275., 1566., 757., 774., 2190., 822., 1355.,
2152., 1575., 1064.],
[1597., 1329., 0., 1617., 1106., 1345., 1951., 1551., 1938.,
1270., 629., 2320., 1646., 1619., 862., 2267., 1357., 934.,
1264., 687., 342.]])
updated = np.insert(data, 0, 0, axis=1)
updated = np.append(updated, [[0] * 22], axis=0)
print(updated)
Output:
[[ 0. 0. 2073. 2352. 1119. 2074. 1344. 4035. 1980. 2213. 2363. 2655.
2322. 1148. 2046. 2234. 1076. 1647. 2957. 1968. 2246. 1723.]
[ 0. 1517. 0. 891. 1537. 1993. 2231. 2574. 689. 1561. 2157. 1517.
3275. 1566. 757. 774. 2190. 822. 1355. 2152. 1575. 1064.]
[ 0. 1597. 1329. 0. 1617. 1106. 1345. 1951. 1551. 1938. 1270. 629.
2320. 1646. 1619. 862. 2267. 1357. 934. 1264. 687. 342.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
Explanation:
- We have inserted in
axis1 to add0in the existing multidimensional array. - We have appended the list of 22 0's to
axis0 at the end.
References:
answered Jan 14, 2022 at 3:33
arshovon
13.7k9 gold badges55 silver badges72 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-py