python - 运行 RegEx 代码块时,Spyder 控制台不断重启
问题描述
我正在准备一个代码来识别名称中带有“日期”一词的数据框中的列。我正在使用 RegEx 比较使用 re.split() 函数从原始名称生成的子字符串。这是整个代码:
import pandas as pd
import numpy as np
import re
df = pd.read_excel(r'C:\Users\rishi\Desktop\PGDBDA\Dataset\Dataset for Date Operations.xlsx')
#print(df)
# Dataset is loaded into Pandas dataframe
column_name = [names for names in df.columns]
#print(column_name)
# The column names are extracted into a list called column_name.
# We plan use a mechanism to identify the sub-string 'date' from the elements in column_name.
name_split = []
for index in column_name:
name_split.append(re.split(' |-|_',index))
#print(name_split)
# Using RegEx we are able to split the elements in the column name based on a set of dilimiters.
# We are grouping them in a list of lists nammed as name_split.
column_index = []
column_count = 0
regex_pattern = re.compile(r"\bdate\b", re.IGNORECASE)
for index in name_split:
for elements in index:
if re.search(regex_pattern, elements) != None:
column_index.append(column_count)
exit()
column_count+=1
print(column_index)
# Will tell us all the columns with 'date' in their names, by stating the index no of the column.
问题是每次我运行这部分代码时:
column_index = []
column_count = 0
regex_pattern = re.compile(r"\bdate\b", re.IGNORECASE)
for index in name_split:
for elements in index:
if re.search(regex_pattern, elements) != None:
column_index.append(column_count)
exit()
column_count+=1
print(column_index)
# Will tell us all the columns with 'date' in their names, by stating the index no of the column.
控制台不断崩溃和重新加载。对此问题的任何见解都将受到高度赞赏。
解决方案
推荐阅读
- jquery - j-query 表单验证代码使页面重定向
- java - 导入大型 JSON 文件时如何修复内存不足
- c# - 如何在 MSTest 中附加测试失败的屏幕截图?
- xaml - 在点击事件函数中的 Listview 内的图像上的 ListView 中隐藏和显示网格
- docker - 在使用 docker 运行 jmeter 进行分布式负载测试时,是否可以定义每个从机上应该运行多少负载
- php - 如何让 API PHP 检查该值是否存在于另一个表中然后插入?
- php - PHP - 通过下载返回 API 调用者响应
- dart - 如何在 Flutter 中删除字符串上的点(。)?
- spring-boot - 在 Spring Boot 中更改 serverTimezone
- python - 龙卷风异步收益率