Python Spyder中的语法无效

Python Spyder中的语法无效,python,pandas,spyder,Python,Pandas,Spyder,我试图根据名称中的某些字符串(如“date”)列出列名 date_cols = [col in df.columns if 'date' in col] 为什么这个命令在spyder上给了我无效的语法 您可以使用for..in循环,条件如下 date_cols = [col for col in df.columns if 'date' in col] 您需要的是: date_cols = [col for col in df.columns if 'date' in col] 语法是:

我试图根据名称中的某些字符串(如“date”)列出列名

date_cols = [col in df.columns if 'date' in col]
为什么这个命令在spyder上给了我无效的语法


您可以使用
for..in
循环,条件如下

date_cols = [col for col in df.columns if 'date' in col]
您需要的是:

date_cols = [col for col in df.columns if 'date' in col]
语法是:

[*expression* for *target* in *iterable* if *conditional*]
其中表达式是可以使用一个或多个目标名称或任何其他值的任何有效表达式;目标是一个或多个名称,由ColumnS分隔;iterable是任何iterable,包括list/set/dictionary/dataframe等;条件表达式是基于一个或多个目标名称的真/假表达式