# Replace rows an columns by zeros in a numpy array

Hi expers.

¿How can I replace rows an columns by zeros (or other values) in a numpy array?

Waiting for your answers.

Thanks a lot.

Replace rows an columns by zeros in a numpy array

Hi expers.

¿How can I replace rows an columns by zeros (or other values) in a numpy array?

Waiting for your answers.

Thanks a lot.

add a comment

1

```
sage: import numpy as np
```

To initialize the array, input python floats (with trailing `r`

): numpy converts
them to numpy floats while it would treat Sage reals as Python objects.

```
sage: a = np.array([[1.r, 2.r, 3.r], [4.r, 5.r, -2.r],[6.r, 7.r, 9.r]])
sage: print a
[[ 1. 2. 3.]
[ 4. 5. -2.]
[ 6. 7. 9.]]
```

Replace line 1 by ones.

```
sage: a[1] = np.ones(3)
sage: print a
[[ 1. 2. 3.]
[ 1. 1. 1.]
[ 6. 7. 9.]]
```

Replace column 1 by zeros.

```
sage: a[:,1] = np.zeros(3)
sage: print a
[[ 1. 0. 3.]
[ 1. 0. 1.]
[ 6. 0. 9.]]
```

Replace line 0 by chosen values. Trailing `r`

no longer needed, numpy converts
Sage reals to the type of other elements of the array.

```
sage: a[0] = [3.,3.,6.]
sage: print a
[[ 3. 3. 6.]
[ 1. 0. 1.]
[ 6. 0. 9.]]
```

Asked: **
2013-07-29 05:13:08 -0600
**

Seen: **16,375 times**

Last updated: **Jul 29 '13**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.