Program for process automation

Asked

Viewed 94 times

0

I’m writing a program in Python to automate one of my work routines.

But I came across a part where I can’t go on, look:

At this stage, I need to write a code that identifies the lowest value of columns 1, 3 and 5 in each row of the DataFrame below, and when you find the lowest value, return the string contained in the next column, and in the same row, of the value found, and save it all in a second DataFrame.

DataFrame that I am using (pandas) as an example in this case is the following:

Data Frame DF3

Link to the data: Data(google drive)

The expected result here would be a dataframe containing the following information:

4bio
expressa
4bio
4bio
4bio
4bio

But even searching for several days, I am far from my goal. So far, the most I could write, and it hasn’t worked by a long shot:

    resultado = []
for i in df3:
    if i == df3.min (axis = 1):
        resultado.append(i)

I ask you to have a little understanding, it’s the first program I try to write, and I’m really caught up in this task.

Thank you so much.

  • If the data were in the form of text I would even respond, because it would only be the case of copying and pasting the data create a Dataframe and apply the solution. But as it is an image would have to write value after value, line after line to there then solve the problem.

  • Good afternoon Augusto. I tried to paste in table format but had lost formatting so I thought it would be easier as picture, I’m sorry I will try again:

  • vl_proposto_4bio distribuidor_4bio vl_proposto_expressa distribuidor_expressa vl_proposto_pfizer distribuidor_pfizer 50 4bio 70 expressa 50 Pfizer 80 4bio 40 expresses 80 Pfizer 30 4bio 50 expresses - Pfizer 1200 4bio 1300 expresses 1200 Pfizer 300 4bio 500 express 300 Pfizer 200 4bio 250 express - Pfizer

  • I have uploaded the data to the link: https://drive.google.com/file/d/1iVTxpYRru9A9QQQIfjXWe_lxSlnhny_o/view?usp=sharing I will include in the question.

1 answer

0

If I understand what you want to do, I have found the following solution:

resultado = []
for row in range(0, len(df.index)): 
    menor = df.iloc[row, 0]
    col_menor = 0
    for col in range(0, len(df.columns), 2):
        if df.iloc[row, col] != '-' and df.iloc[row, col] < menor:
            menor = df.iloc[row, col]
            col_menor = col
    resultado.append(df.iloc[row, col_menor + 1])

The first for passes through each Dataframe line (I used a range to ensure that they are numeric values if you choose to change the index). The variable menor starts with the first value of that line.

The second for passes between columns, a yes, a no, to jump those that have only strings.

The variable col_menor stores the position of the column containing the lowest value in that row.

After checking if it is the lowest number (if different from ' - '), the value of the column after the lowest number is passed to the list resultado.

Note: In your dataframe there are equal values in the same row, in different columns, note this.

  • My dear, I can’t thank you enough for your help, it’s exactly what I needed.

  • I applied the code and it worked perfectly. I will now study this code to understand perfectly what it is doing. One last doubt: I am expected to have equal values on the same line (different suppliers with the same proposal). In this case, he will consider the first value found, ie the nearest to column 0, right? Thanks again.

  • Yes, because it only records if the value is less than what is in the variable, if it is equal, it leaves the IF without registering the value. If any further questions arise, please comment again that I will do my best to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.