【问题标题】:Find all non-latin1 (ISO 8859-1) characters in dataframe在数据框中查找所有非 latin1 (ISO 8859-1) 字符
【发布时间】: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


    【解决方案1】:

    我想出了这个。我知道它可能需要一些清理,或者可以像 Karl 建议的那样应用。但它现在有效。

    import pandas as pd
    
    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']
        })
    
    islatin=[]
    
    def latin1(df, column):
        islatin1 = lambda s: bool(s.encode(encoding="latin-1", errors="strict"))
        for index, row in df.iterrows():
            if row[column]:
                try:
                    islatin1(row[column])
                except UnicodeEncodeError:
                    islatin.append([row['ID'],column, row[column]])    
    
    
    for x in DF.columns.values.tolist():
        try:
            latin1(DF, x)
        except (TypeError, AttributeError):
            continue
    
    latin_chars = pd.DataFrame(islatin, columns=['ID', 'Bad_Column', 'Bad_Data'])
    print(latin_chars)
    

    【讨论】:

      猜你喜欢
      • 2013-01-19
      • 2016-11-12
      • 2023-03-27
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 2014-01-17
      相关资源
      最近更新 更多