0

I have a 2D array, and I would like to find the min. value in every column and minus this min value in every column.

For example,

array = [
[1, 2, 4],
[2, 4, 6],
[5, 7, 9]]

The smallest values in columns are 1, 2, 4.

I would like the result to be

array = [
[0, 0, 0],
[1, 2, 2],
[4, 5, 5]]

How can I achieve this?

2
  • 1
    if you have real numpy.array() or pandas.DataFrame() then you could use array.min() or maybe array.min(axis=???). And maybe even array = array - array.min() Commented Mar 11, 2020 at 4:21
  • 1
    Please follow the numpy documenation for basics array-array.min(0) Commented Mar 11, 2020 at 4:22

1 Answer 1

2

If you use real numpy.array or pandas.DataFrame then you have arr.min(axis=0) and arr - arr.min(axis=0)


For numpy.array

import numpy as np

data = [
    [1, 2, 4],
    [2, 4, 6],
    [5, 7, 9]
]

arr = np.array(data)

print( arr.min(axis=0) )

print( arr - arr.min(axis=0) )

Result

[1 2 4]

[[0 0 0]
 [1 2 2]
 [4 5 5]]

Similar for pandas.DataFrame

import pandas as pd

data = [
    [1, 2, 4],
    [2, 4, 6],
    [5, 7, 9]
]

df = pd.DataFrame(data)

print( df.min(axis=0) )

print( df - df.min(axis=0) )

Result

0    1
1    2
2    4
dtype: int64

   0  1  2
0  0  0  0
1  1  2  2
2  4  5  5
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.