2 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
使用np.where+Series.isin
import numpy as np
my_list=['goal', 'detail', 'objective']
df['Col2'] = np.where(df.Col1.isin(my_list), 1, 0)
或如@Ch3steR 所述
df['Col2'] = df.Col1.isin(my_list).astype('int')
Col1 Col2
0 code 0
1 goal 1
2 python 0
3 detail 1

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
如何申請(qǐng):
import pandas as pd
df = pd.DataFrame(data = ['code', 'goal', 'python', 'detail'], columns = ['Col1'])
mylist=['goal', 'detail', 'objective']
df['Col2'] = df['Col1'].apply(lambda cell: 1 if cell in mylist else 0)
給
? ? ?Col1? Col2
0? ? code? ? ?0
1? ? goal? ? ?1
2? python? ? ?0
3? detail? ? ?1
添加回答
舉報(bào)