0

Actually, i'm very new to python and working on some image problem statement. Stuck in a problem and not able to get out of this.

I have data frame like:

Image                       RGB                 max_1 max_2 max_3
file1   [[224,43,234][22,5,224][234,254,220]]     234   224   254
file2   [[22,143,113][221,124,224][234,254,123]]  143   224   254
file3   [[44,45,2][2,5,4][34,254,220]]             45     5   254
file4   [[224,243,34][22,5,24][24,25,20]]         243    24    25
file5   [[210,13,34][22,5,224][234,254,220]]      210   224   254

I tried np.max() but it gave me unexpected results that means for the first row this gave me output 254, and so on.

I'm expecting the output as column max_1, max_2, and max_3.

1
  • What output do you expect? Commented Apr 15, 2019 at 5:39

6 Answers 6

3

Using list-comprehension:

a = np.array([[224,43,234], [22,5,224], [234,254,220]])

print([x.max() for x in a])

OUTPUT:

[234, 224, 254]
Sign up to request clarification or add additional context in comments.

Comments

2

I'll assume that you want the max values of R, G and B respectively. If you want this then, here is one way to do it:

a = np.array([ [224,43,234], [22,5,224], [234,254,220]])
r_max = a.T[0].max()
g_max = a.T[1].max()
b_max = a.T[2].max()

Comments

1

You can do something like this perhaps:

file1 = [[224,43,234],[22,5,224],[234,254,220]]

for idx, inner_list in enumerate(file1):
    print('max_'+str(idx+1)+' : '+str(max(inner_list)))

Comments

1

Another way:

import numpy as np

a=np.array([[1,2,3],[11,12,13],[21,22,23]])
print(a)

maxInRows = np.amax(a, axis=1)
print('Max value of every Row: ', maxInRows)

1 Comment

You can use axis to fetch the max value, use axis=0 to get the max value from a column or else use asix=1 to get the max value from the row.
0

You said you have a data frame, so I assume it's a pandas DataFrame object. In which case, you can use list comprehension to take the max from each sub-list in the list, and assign each element to a new column (this loop isn't elegant but will work):

df['max_colors'] = df['RGB'].apply(lambda x: [np.max(color) for color in x])
for i in range(3):
    df['max_'+str(i)] = df['max_colors'].apply(lambda x: x[i])

Comments

0

Solved the problem like this. Thanks for having all the answers.

df['max_1'] = 0
df['max_2'] = 0
df['max_3'] = 0
for i in range(5):
    df['max_1'][i] = df['RGB'][i][0].max()
    df['max_2'][i] = df['RGB'][i][1].max()
    df['max_3'][i] = df['RGB'][i][2].max()

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.