0

I have created a pandas dataframe that I want to apply a couple of functions, to create two new columns based on the values of a column in the dataframe. Let's say the original dataframe is like this:

    name    sum
0   attr_1  900
1   attr_2  20000
2   attr_3  60000

I want to create a function that creates two new columns based on the sum column like this:

    name    sum      score    rank
0   attr_1  900      0        low
1   attr_2  20000    3        med
2   attr_3  60000    7        high

Right now I have a simple if statement that goes row by row and assigns the score column, and then I would create the same logic for the score column to create the rank column:

def score(row):
    val = None
    if row['count_sum'] < 1000:
        val = 0
    elif 1000 < row['count_sum'] < 30000:
        val = 3
    elif 30000 < row['count_sum'] < 70000:
        val = 7
    else:
        val = 10

df['score']=df.apply(score, axis=1)

This is just a small snippet of the logic, but the dataframe can get big, and going through if statements twice over can be costly. I was wondering if there is a way to use indexes or lists to assign these values dynamically in one go.

Any help is really appreciated!

mlenthusiast
  • 1,094
  • 1
  • 12
  • 34

0 Answers0