import pandas as pd
import re
# 示例数据
df = pd.read_excel('C:/Users/Administrator/Desktop/0910.xlsx')
# 定义一个函数,用于判断字符串是否为身份证号码
def is_id_number(id_number):
if len(id_number) == 18:
if id_number[:-1].isdigit() and (id_number[-1].isdigit() or id_number[-1].upper() in ['X', 'x']):
return True
return False
# 使用apply方法,将函数应用到每个单元格,并将结果存储在新的列中
df['Has_ID_Number'] = df['C'].apply(is_id_number)
print(df)
KeyError Traceback (most recent call last)
File C:\ProgramData\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3802 , in Index.get_loc (self, key, method, tolerance)
3801 try :
-> 3802 return self . _engine . get_loc ( casted_key )
3803 except KeyError as err:
File C:\ProgramData\anaconda3\lib\site-packages\pandas\_libs\index.pyx:138 , in pandas._libs.index.IndexEngine.get_loc ()
File C:\ProgramData\anaconda3\lib\site-packages\pandas\_libs\index.pyx:165 , in pandas._libs.index.IndexEngine.get_loc ()
File pandas\_libs\hashtable_class_helper.pxi:5745 , in pandas._libs.hashtable.PyObjectHashTable.get_item ()
File pandas\_libs\hashtable_class_helper.pxi:5753 , in pandas._libs.hashtable.PyObjectHashTable.get_item ()
KeyError : 'C'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Cell In[10], line 20
16 df = pd . read_excel( ' C:/Users/Administrator/Desktop/0910.xlsx ' )
18 # 在df 中创建一个新的列'Is_ID_Card',并使用apply 方法和之前定义的is_id_card 函数,将每一行的'1' 列的值作为参数传入,判断其是否是身份证号码
19 # 结果是True 或False,分别写入'Is_ID_Card' 列
---> 20 df[ ' Is_ID_Card ' ] = df [ ' C ' ] . apply(is_id_card)
22 # 将处理后的数据保存到新的Excel 文件'output.xlsx' 中,其中index=False 表示不保存索引列
23 df .to_excel(' output.xlsx ' , index = False )
File C:\ProgramData\anaconda3\lib\site-packages\pandas\core\frame.py:3807 , in DataFrame.__getitem__ (self, key)
3805 if self . columns . nlevels > 1 :
3806 return self . _getitem_multilevel(key )
-> 3807 indexer = self . columns . get_loc ( key )
3808 if is_integer(indexer):
3809 indexer = [indexer]
File C:\ProgramData\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3804 , in Index.get_loc (self, key, method, tolerance)
3802 return self . _engine . get_loc(casted_key)
3803 except KeyError as err:
-> 3804 raise KeyError (key) from err
3805 except TypeError :
3806 # If we have a listlike key, _check_indexing_error will raise
3807 # InvalidIndexError. Otherwise we fall through and re-raise
3808 # the TypeError.
3809 self . _check_indexing_error(key)
KeyError : 'C'
判断EXCEL单元格内是否有身份证号码,结果在新增一列中用TRUE或FALSE表示,请问这个代码哪里有问题,这个列名咋写都不对
【以下回答由 GPT 生成】
答案:
在代码中遇到的KeyError
异常是由于使用了无效的列标签'C'
。根据给出的示例数据,我假设身份证号码所在的列标签为'ID_Number'
。
修复以上代码问题的方法是将df['C']
改为df['ID_Number']
。
以下是修复后的代码:
import pandas as pd
import re
# 示例数据
df = pd.read_excel('C:/Users/Administrator/Desktop/0910.xlsx')
# 定义一个函数,用于判断字符串是否为身份证号码
def is_id_number(id_number):
if len(id_number) == 18:
if id_number[:-1].isdigit() and (id_number[-1].isdigit() or id_number[-1].upper() in ['X', 'x']):
return True
return False
# 使用apply方法,将函数应用到每个单元格,并将结果存储在新的列中
df['Has_ID_Number'] = df['ID_Number'].apply(is_id_number)
print(df)
运行流程如下:
pandas
和re
模块。pd.read_excel
函数读取Excel文件,将数据存储在df
中。is_id_number
函数,用于判断输入的字符串是否为有效的身份证号码。身份证号码的有效性判断根据以下条件:True
,否则返回False
。df['ID_Number'].apply(is_id_number)
,将is_id_number
函数应用到名为'ID_Number'
的列的每个单元格,返回一个包含布尔值的Series对象。'Has_ID_Number'
的新列,并将步骤4中的Series对象赋值给该列。df
,包含判断结果的新列Has_ID_Number
。以上就是修复后的代码和代码的运行流程。
【相关推荐】