[英]Exception in Pandas data frame apply function
I am getting the following error and I am not sure what it is. 我收到以下错误,我不确定这是什么。 Can you please help me identify what is causing this error? 您能帮我确定造成此错误的原因吗?
def determine_device_type(*args, **kwargs):
device_name = str(args[0])
#print(device_name)
if re.search(r'^\w+c(\d\d|\w\w|\d\d\d\d)$', device_name):
print(device_name, ' --> ', 'Cisco')
return 'Cisco'
if re.search(r'^\w+r(\d\d|\w\w|\d\d\d\d)$', device_name):
print(device_name, ' --> ', 'Riverbed')
return 'Riverbed'
if re.search(r'^\w+j(\d\d|\w\w|\d\d\d\d)$', device_name):
print(device_name, ' --> ', 'Juniper')
return 'Juniper'
return ''
vendor = df['DEVICE'].apply(determine_device_type)
df.insert(3, 'VENDOR', vendor)
Error 错误
ValueError Traceback (most recent call last)
<ipython-input-42-8b32a12d4761> in <module>()
21
22 vendor = df['MDN_DEVICE'].apply(determine_device_type)
---> 23 df.insert(3, 'VENDOR', vendor)
24 df
C:\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in insert(self, loc, column, value, allow_duplicates)
2611 value = self._sanitize_column(column, value, broadcast=False)
2612 self._data.insert(loc, column, value,
-> 2613 allow_duplicates=allow_duplicates)
2614
2615 def assign(self, **kwargs):
C:\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in insert(self, loc, item, value, allow_duplicates)
4061 if not allow_duplicates and item in self.items:
4062 # Should this be a different kind of error??
-> 4063 raise ValueError('cannot insert {}, already exists'.format(item))
4064
4065 if not isinstance(loc, int):
ValueError: cannot insert VENDOR, already exists
It seems you already have a 'VENDOR' column. 看来您已经有一个“供应商”列。 You can use the df.head() to confirm. 您可以使用df.head()进行确认。 To avoid the error, drop the column before inserting. 为避免该错误,请在插入之前删除该列。
df.drop(columns=['VENDOR']).insert(3, 'VENDOR', vendor)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.