Home > Net >  data frame and list operation
data frame and list operation

Time:07-15

There are three columns in df: mins, maxs, and col. I would like to generate a binary list according to the following rule: if col[i] is smaller than or equal to mins[i], add a "1" to the list and keep adding "1" for each row until col[i n] is greater than or equal maxs[i n]. After reaching maxs[i n], add "0" to the list for each row until finding the next row where col[i] is smaller than or equal to mins[i]. Repeat this entire process till going over all rows.

For example,

col  mins maxs
 2    1    6   (0)
 4    2    6   (0)
 2    3    7   (1)
 5    5    6   (1)
 4    3    8   (1)
 4    2    5   (1)
 5    3    5   (0)
 4    0    5   (0)
 3    3    8   (1)
......

So the list would be [0,0,1,1,1,1,0,0,1]. Does this make sense?

I gave it a shot and wrote the following, which unfortunately did not achieve what I wanted.

def get_list(col, mins, maxs):
    l = []
    i = 0
    while i <= len(col):
        if col[i] <= mins[i]:
            l.append(1)
            while col[i 1] <= maxs[i 1]:
                l.append(1)
                i  = 1
            break
        break
    return l

Thank you so much folks!

CodePudding user response:

My answer may not be elegant but should work according to your expectation.

  1. Import the pandas library.
    import pandas as pd
    
  2. Create dataframe according to data provided.
    input_data = {
        'col': [2, 4, 2, 5, 4, 4, 5, 4, 3],
        'mins': [1, 2, 3, 5, 3, 2 , 3, 0, 3],
        'maxs': [6, 6, 7, 6, 8, 5, 5, 5, 8]
    }
    dataframe_ = pd.DataFrame(data=input_data)
    
  3. Using a for loop iterate over the rows. The switch variable will change accordingly depending on the conditions was provided which results in the binary column being populated.
     binary_switch = False
    
     for index, row in dataframe_.iterrows():
    
         if row['col'] <= row['mins']:
             binary_switch = True
         elif row['col'] >= row['maxs']:
             binary_switch = False
    
         binary_output = 1 if binary_switch else 0
    
         dataframe_.at[index, 'binary'] = binary_output
    
     dataframe_['binary'] = dataframe_['binary'].astype('int')
     print(dataframe_)
    
  4. Output from code.
         col   mins  maxs  binary
     0    2     1     6       0
     1    4     2     6       0
     2    2     3     7       1
     3    5     5     6       1
     4    4     3     8       1
     5    4     2     5       1
     6    5     3     5       0
     7    4     0     5       0
     8    3     3     8       1
    

CodePudding user response:

Your rules give the following decision tree:
1: is col <= mins?
True: l.append(1)
False: next question
2: was col <= mins before?
False: l.append(0)
True: next question:
3: is col >= maxs?
True: l.append(0)
False: l.append(1)

Making this into a function with an if/else tree, you get this:

def make_binary_list(df):
    l = []
    col_lte_mins = False
    for index, row in df.iterrows():
        col  = row["col"]
        mins = row["mins"]
        maxs = row["maxs"]

        if col <= mins:
            col_lte_mins = True
            l.append(1)
        else:
            if col_lte_mins:
                if col >= maxs:
                    col_lte_mins = False
                    l.append(0)
                else:
                    l.append(1)
            else:
                l.append(0)
    return l

make_binary_list(df) gives [0, 0, 1, 1, 1, 1, 0, 0, 1]

  • Related