您好,我有一个df,其中后缀通常是文件名。
df apple_filename_fruit.csv banan_filename_new fruity.csv test_col
0 0 26 4
1 23 262 2
2 23 2626 3
既然没有打印,正则表达式应该如何修改?
for col in df.columns:
if col.endswith("filename_(w+)") and "new" in col:
print(col)
您可以使用filter
和regex
参数。
df.filter(regex=r'(?=.*filename)(?=.*new)')
banan_filename_new
0 26
1 262
2 2626
用Series.str.contains
和$
作为字符串的结尾,用&
作为链的结尾,AND
:
m = df.columns.str.contains("filename_.*$") & df.columns.str.contains("new")
print (m)
[False False True False False]
for c in df.columns[m]:
print (c)
banan_filename_new
我不认为endswith
与正则表达式工作;编辑for循环的一种方法是使用re
模块,并嵌套if语句:
for col in df:
if re.search("filename.*$", col):
if "new" in col:
print(col)
banan_filename_new