问题
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)
回答1:
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
回答2:
A map version:
First, create a dictionary:
d = {**{o: "odd" for o in odd_numbers}, **{e: "even" for e in even_numbers}}
Then use map on the series:
df['odd_or_even'] = df['number'].map(d)
来源:https://stackoverflow.com/questions/37976823/how-to-conditionally-update-dataframe-column-in-pandas-based-on-list