Christopher Jenkins Christopher Jenkins - 5 months ago 103
Python Question

How to conditionally update DataFrame column in Pandas based on list

Supposed I have a dataframe with one column:

df = pd.DataFrame(np.random.randint(0,9,size=(100, 1)), columns=['number'])


I have two lists, one list containing even numbers, the other containing odd numbers.

odd_numbers = [1,3,5,7,9]
even_numbers = [0,2,4,6,8]


I'd like to create another series on the dataframe that says 'even' or 'odd' depending on the value within df['number']

Something like:

df['odd_or_even'] = 'even' if df[number].isin(even_numbers)
df['odd_or_even'] = 'odd' if df[number].isin(odd_numbers)

Answer

I think you can use numpy.where:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,9,size=(100, 1)), columns=['number'])

df['odd_or_even'] = np.where(df.number % 2, 'odd', 'even')
print (df)

    number odd_or_even
0        1         odd
1        0        even
2        4        even
3        5         odd
4        0        even
5        0        even
6        1         odd
7        0        even
8        7         odd
9        8        even

EDIT by comment

Use loc:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,9,size=(20, 1)), columns=['number'])

odd_numbers = [1,3,5,7,9]
even_numbers = [0,2,4,6,8]

df.loc[df.number.isin(odd_numbers), 'odd_or_even'] = 'odd'
df.loc[df.number.isin(even_numbers), 'odd_or_even'] = 'even'

print (df)
    number odd_or_even
0        5         odd
1        1         odd
2        2        even
3        3         odd
4        5         odd
5        6        even
6        3         odd
7        4        even
8        2        even
9        8        even
10       8        even
11       1         odd
12       2        even
13       1         odd
14       3         odd
15       3         odd
16       5         odd
17       4        even
18       2        even
19       5         odd