PANDAS DROP ROWS基于过滤的项目,我的解决方案 - 不满意

时间:2016-05-17 19:27:49

标签: python pandas text-analysis

我正在努力清理列表域名。

我想删除某些“符合”标准的行。我成功地确定了第一个标准,第二个标准很容易做到。

但是,我不能删除行。我已经尝试了几种解决方案,但我所拥有的最好的是以下内容。

from wordsegment import segment
import pandas as pd

def assignname():
    dfr = pd.read_csv('data.net.date.csv')

    for domainwtld in dfr.domain:
        dprice = dfr.price
        domainwotld = domainwtld.replace(".net", "")
        seperate = wordsegment.segment(domainwotld)
        dlnt = (min(seperate, key=len))
        slnt = len(dlnt)
        if slnt <= 1:
            baddomains = domainwtld
            a = dfr.loc[dfr['domain'] < (baddomains)]
            print (a)

当我运行此代码时,我收到一个输出,在删除“baddomains”中的第一个项目后,在“dfr”中打印整个项目。它会在循环完成之前执行此操作。

如何根据baddomains过滤“原始”csv文件?

1 个答案:

答案 0 :(得分:0)

from wordsegment import segment
import pandas as pd

url = 'http://download1474.mediafire.com/3ndc8vevwtng/sa4ifz8rixe7m8u/data.net.date+%285%29.csv'
dfr = pd.read_csv(url)
dfr['domain'] = dfr.domain.str.replace(".net", "")
dfr['words'] = df.domain.apply(segment)
good_domains = dfr[dfr.words.apply(lambda words: len(min(words, key=len))) > 1]
bad_domains = dfr[~dfr.domain.isin(good_domains.domain)]

>>> bad_domains
        domain  price           words
2        keeng    700       [keen, g]
14       ymall    777       [y, mall]
22       idisc    850       [i, disc]
26      borsen    877      [borse, n]
38    cellacom    895  [cell, a, com]
51     iwealth    999     [i, wealth]
96     iplayer   1500     [i, player]
116  mcommerce   2000   [m, commerce]
118      apico   2052       [a, pico]
134     epharm   2500      [e, pharm]
139     ionica   2579      [ionic, a]
153    kasiino   2999   [kasi, in, o]
155    alpadia   3000   [al, padi, a]
158   similans   3152    [similan, s]
163    ifuture   3499     [i, future]

>>> bad_domains.domain.tolist()
['keeng',
 'ymall',
 'idisc',
 'borsen',
 'cellacom',
 'iwealth',
 'iplayer',
 'mcommerce',
 'apico',
 'epharm',
 'ionica',
 'kasiino',
 'alpadia',
 'similans',
 'ifuture']