Python重新应用/搜索类型错误:“非类型”对象不可下标

Python重新应用/搜索类型错误:“非类型”对象不可下标,python,pandas,re,Python,Pandas,Re,有人能解释我为什么会出现这个错误以及如何修复它吗?我正在尝试搜索98-99年的标题,我想获得98年的第一部分: 示例标题:汽车EBC 98-99 TypeError: 'NoneType' object is not subscriptable 在年_min行是错误发生的地方 import pandas as pd import re fileinString = 'a.csv' df1 = pd.read_csv(fileinString, sep=",") # split title

有人能解释我为什么会出现这个错误以及如何修复它吗?我正在尝试搜索98-99年的标题,我想获得98年的第一部分:

示例标题:汽车EBC 98-99

TypeError: 'NoneType' object is not subscriptable
在年_min行是错误发生的地方

import pandas as pd
import re

fileinString = 'a.csv'

df1 = pd.read_csv(fileinString, sep=",")

# split title of df1 into string and year tag min and year tag max
regular_expression = re.compile(r'\d\d-\d\d')

title_string = df1['*Title']


year_min = title_string.apply(lambda x: regular_expression.search(x)[0].split('-')[0])

year_max = df1['*Title'].apply(lambda x: regular_expression.search(x)[0].split('-')[1])

print(year_min)
关闭it运行但不起作用的示例:

如果试图使用括号表示法从一个不包含任何内容的变量访问数据,则会出现典型的异常

x = None
x[0]
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

您遇到的异常典型地是试图使用括号表示法从一个不包含任何内容的变量访问数据

x = None
x[0]
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

这是因为df1['*Title']的值与此模式不匹配

当它在字符串中找到模式时,会重新调用某些内容

In [18]: regular_expression = re.compile(r'\d\d-\d\d')

In [19]: regular_expression.search('12-18')

Out[19]: <_sre.SRE_Match object; span=(0, 5), match='12-18'>
没有一个是不可下标的 i、 e.你什么都不能做[0]

所以最终你要做的就是

In [21]: None[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-4b6604f77809> in <module>
----> 1 None[0]

TypeError: 'NoneType' object is not subscriptable

这是因为df1['*Title']的值与此模式不匹配

当它在字符串中找到模式时,会重新调用某些内容

In [18]: regular_expression = re.compile(r'\d\d-\d\d')

In [19]: regular_expression.search('12-18')

Out[19]: <_sre.SRE_Match object; span=(0, 5), match='12-18'>
没有一个是不可下标的 i、 e.你什么都不能做[0]

所以最终你要做的就是

In [21]: None[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-4b6604f77809> in <module>
----> 1 None[0]

TypeError: 'NoneType' object is not subscriptable

请提供一份报告。我们不知道您的数据框中有什么。非常简单:如果re.search没有找到匹配项,它将返回None。请提供一个。我们不知道您的数据框中有什么。非常简单:如果re.search没有找到匹配项,它将返回None。因此,我删除了分割函数,因此我只运行以下行:year_min=df1['*Title']。applylambda x:regular_expression.searchx,这将生成以下格式的结果:0我如何才能1。在获取信息时,不要使用“重新匹配”对象。您可以使用组方法访问数据,如:match.group0,其中0是组的索引,因为您可以匹配多个组。我为您的问题添加了一个可能但不是最佳的解决方案。因此,我删除了拆分函数,因此我只运行以下行:year_min=df1['*Title'].applylambda x:regular_expression.searchx,这将生成以下格式的结果:0我如何才能1。在获取信息时,不要使用“重新匹配”对象。您可以使用组方法访问数据,如:match.group0,其中0是组的索引,因为您可以匹配多个组。我为您的问题添加了一个可能但不是最佳的解决方案。