简体   繁体   English

“y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)”的含义

[英]Meaning of "y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)"

This Syntax is the second line after uploading a csv file by pandas library and get_dummies drop, i want to understand this syntax better in order to make use of it thank !这个语法是通过 pandas 库和 get_dummies 上传 csv 文件后的第二行,我想更好地理解这个语法以便使用它谢谢!

y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)

Your code means:您的代码意味着:

 y = df['something'].apply(lambda x: 1 if x== 'yes' else 0)

Test values in column something and return 1 if match yes else return 0 in new Series in variable y .测试something列中的值, yes匹配则返回 1,否则在变量y的新Series中返回 0。

Btw, vectorized solution is:顺便说一句,矢量化解决方案是:

 #return Series
 y = (df['something'] == 'yes').astype(int)

 #return 1d array
 y = np.where(df['something']== 'yes', 1, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM