1

I'm working on a data table and I need to create a new column based on which of the classes the value of another column falls in.

This is the original table:

ID  sequence
AJ8    2
FT7    3
JU4    5
ER2    3
LI5    2
FR2    7
WS1    8
UG4    9

The ranges are 2, 3, 4, 6: first; 1,5,0: second; and 7,8, 9: third.

I created the variables

first  = ['2', '3', '4', '6']
second = ['1', '5', '0']
third  = ['7', '8', '9']

I want to get the following table

ID  sequence    code
AJ8    2        FIRST
FT7    3        FIRST
JU4    5        SECOND
ER2    3        FIRST
LI5    2        FIRST
FR2    7        THIRD
WS1    8        THIRD
UG4    9        THIRD

How do I do this?

gboffi
  • 22,939
  • 8
  • 54
  • 85
John_gis
  • 117
  • 8

1 Answers1

1

I would create a function that conditionally returns the value you want.

import pandas as pd

keys = ['AJ8', 'FT7', 'JU4', 'ER2', 'LI5', 'FR2', 'WS1', 'UG4']
values = [2, 3, 5, 3, 2, 7, 8, 9]

df = pd.DataFrame(list(zip(keys, values)), columns =['key', 'value'])

def get_new_column(df):
    if df['value'] in [2, 3, 4, 6]:
        return 'first'
    elif df['value'] in [1, 5, 0]:
        return 'second'
    elif df['value'] in [7, 8, 9]:
        return 'third'
    else:
        return ''

df['new'] = df.apply(get_new_column, axis=1)
print(df)

Output:

   key  value     new
0  AJ8      2   first
1  FT7      3   first
2  JU4      5  second
3  ER2      3   first
4  LI5      2   first
5  FR2      7   third
6  WS1      8   third
7  UG4      9   third

Here are more examples.

cwalvoort
  • 1,851
  • 1
  • 18
  • 19