0

here is something that I thought would be easier but is not (for me)...

I have some data as the one bellow:

data = pd.DataFrame({ 
    'column1' : [0, 49, 50, 0, 100], 
    'column2' : [100, 0, 0, 0, 0], 
    'column3' : [0, 51, 50, 100, 0]
})

the output for my needs is better seen like this:

(0,100,0
49,0,51
50,0,50
0,0,100
100,0,0)

I need fix these values as none of then are zero and all rows sum to one. (column1 + column2 + column3 = 100) ex:

(1,98,1
49,1,50
49,1,50
1,1,98
98,1,1)

I thought about some function that would identify the maximum or minimum column, like np.maximum() or np.minimum() but I can't solve for the cases like row 0, row 3 or row 4... Any help would be appreciate..

weare138
  • 21
  • 2
  • Hey, I just typed it that way to be easier to read. It's not to transpose the rows. I can't see how to transpose a 0,100,0 would turn to 1,98,1 – weare138 Nov 24 '20 at 23:28
  • Got it; my apologies. Reopened. – Prune Nov 25 '20 at 00:30
  • Hey, I made a lot of attempts (a lot) that did not work and believed it could make the question harder to read. Next time I'll post with all the code. Tks! – weare138 Nov 25 '20 at 01:35

1 Answers1

0
import pandas as pd


data = pd.DataFrame({ 
'column1' : [0, 49, 50, 0, 100], 
'column2' : [100, 0, 0, 0, 0], 
'column3' : [0, 51, 50, 100, 0]
})

for row in data.values:  #data.values gets all rows of the df
    to_minus = list(row).count(0) #Get num of 0, subtract from max(row)

    for i in range(len(row)):
        elem = row[i]

        if elem == 0:  #If elem is 0 add 1 to elem
        row[i] += 1

        else:
            if row[i] == max(row): # If elem is max(row) subtract num of 0
               row[i] -= to_minus
               to_minus = 0         # Set to minus to 0 in case of duplicates
print(data)
Ashwin
  • 31
  • 3
  • Hey, tks a lot!, it got really clean! I'm quite new in programming and would take me really long to get to this! My code was getting giant and no solving my problem.. – weare138 Nov 25 '20 at 01:37
  • You are welcome, please upvote me and mark the question as answered :) – Ashwin Nov 25 '20 at 01:40