【发布时间】:2021-11-10 02:00:27
【问题描述】:
使用非工作尝试进行编辑
我正在尝试这样的事情,但得到ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
islatin1=[]
for column in DF.columns.values.tolist():
for row in DF.iterrows():
for i in row:
if i:
if type(i) != bool:
if type(i) != float:
try:
i.encode(encoding = "latin-1", errors="strict")
except:
islatin1.append([row['ID'],column, row[column]])
CliffNotes - 需要识别我的数据框中不是 latin 1 的任何数据,以便我可以阻止它进入一个挑剔的数据库。
目前我正在通过以下方式检查任何非 ascii 字符的数据帧:
DF = pd.DataFrame({
'ID':[1,2,5,25,26],
'link1':['apple—', 'www.google.com', 'gm@yahoo.com', 'http://www.youtube.com', '888-555-5556 Ryan Parkes rp@abc.io'],
'link2':['http://www.bing.™com','http://www.linkedin.com','',' please call� now','http://www.reddit.com~|~http://www.youtube.™com~|~http://www.youtube.com' ],
'link3':['http://www.stackoverflow.com~|~http://www.ebay.com', 'http://www.imdb.com�', 'http://www.google.co.uk','more random text that � could be really long and annoying','over the hills and through the woods']
})
def asciifunc(df,column):
isascii = lambda s: len(s) ==len(s.encode())
for index, row in df.iterrows():
if not isascii(row[column]):
aascii_list.append([row['ID'],column, row[column]])
aascii_list=[]
for x in DF.columns.values.tolist():
try:
asciifunc(DF, x)
except TypeError:
continue
Invalid_chars = pd.DataFrame(aascii_list, columns=['ID', 'Bad_Column', 'Bad_Data'])
但现在我需要找到任何非拉丁语 (ISO 8859-1) 字符。 (我有一个数据库,它让我适合我传递的字符,所以我需要标记我的数据框中存在的任何具有非 latin1 字符的数据。(如:™、-、� 等。 )
除了列出所有 latin1 字符的正则表达式之外,我不确定去哪里寻找这些?
编辑:Wiktor 显然不喜欢我在帖子中标记正则表达式,尽管我认为这可能是一个可行的解决方案。
【问题讨论】:
标签: python regex pandas ascii iso-8859-1