Pandas删除基于筛选项的行,我的解决方案不满意

2024-10-04 05:25:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在清理一个域名列表。你知道吗

我想删除某些“符合”条件的行。我已经成功地确定了第一个标准,第二个将很容易做到。你知道吗

但是,我不能删除行。我试过几种解决办法,但我最好的办法是如下。你知道吗

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”中打印整个项目。直到循环完成。你知道吗

如何根据域过滤“原始”csv文件?你知道吗


Tags: csvimportnetlendomainsegmentpddfr
1条回答
网友
1楼 · 发布于 2024-10-04 05:25:03
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']

相关问题 更多 >