I am trying to find the proper python syntax to create a flag with a value of yes
if columnx
contains any of the following numbers: 1, 2, 3, 4, 5
.
def create_flag(df):
if df['columnx'] in (1,2,3,4,5):
return df['flag']=='yes'
I get the following error.
TypeError: invalid type comparison
Is there an obvious mistake in my syntax?
使用np.where
与大熊猫isin
为:
df['flag'] = np.where(df['columnx'].isin([1,2,3,4,5]),'yes','no')
You have lot of problems in your code! i assume you want to try something like this
def create_flag(df):
if df['columnx'] in [1,2,3,4,5]:
df['flag']='yes'
x = {"columnx":2,'flag':None}
create_flag(x)
print(x["flag"])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.