簡體   English   中英

Pandas Dataframe 未在 using.loc 列中返回 1 個字符串值的結果

[英]Pandas Dataframe not returning results for 1 string value in a column using .loc

我目前正在開發一個 discord 機器人,用於我計划在未來制作的 web 應用程序,它利用 Pandas 制作一個 dataframe 實例,該實例將所有可能的 drop 存儲在 WoWW 中。 我創建了這個機器人來獲取用戶輸入,例如“.loot Cloth”來存儲“cloth”作為參數並將其傳遞給 a.loc function 以在“itemtype”列中搜索“cloth”。 我遇到了一個有趣的錯誤,如果我搜索“皮革”,這將不起作用。

這是我的 dataframe 皮革部分的示例:

    itemname                            itemtype    itemslot    stat1    stat2    source
--  ----------------------------------  ----------  ----------  -------  -------  ----------------------------
32  Corpuscular Leather Greaves         leather     feet        crit     mastery  Carapace of N'Zoth
33  Cord of Anguished Cries             Leather     waist       haste    mastery  Dark Inquisitor Xanesh
34  Gloves of Abyssal Authority         leather     hands       haste    mastery  Drest'agath
35  Spaulders of Aberrant Allure        leather     shoulders   azerite           Il'gynoth, Corruption Reborn
36  Belt of Braided Vessels             Leather     waist       haste    vers     Il'gynoth, Corruption Reborn
37  Stygian Guise                       leather     head        azerite           Maut
38  Boots of Manifest Shadow            leather     feet        haste    mastery  Maut
39  Pauldrons of the Great Convergence  leather     shoulders   azerite           N'Zoth the Corruptor
40  Bracers of Dark Prophecy            leather     wrists      crit     haste    Prophet Skitra
41  Macabre Ritual Pants                leather     legs        crit     vers     Prophet Skitra
42  Gibbering Maw                       leather     head        azerite           Ra-den the Despoiled
43  Wristwraps of Volatile Power        leather     wrists      haste    mastery  Shad'har the Insatiable
44  Chitinspine Gloves                  leather     hands       vers     mastery  The Hivemind
45  Darkheart Robe                      leather     chest       azerite           Vexiona
46  Onyx-Imbued Breeches                leather     legs        vers     mastery  Wrathion, the Black Emperor

如您所見,這些項目在 'itemtype' 列中存儲為 'leather' ,其中 2 保存為 'Leather' 以嘗試調試問題。

if message.content.startswith('!loot'):
        arg = message.content.lstrip('!loot ')
        if arg == '1h':
            await message.channel.send('`' + tabulate(df1h, headers='keys', tablefmt='simple') + '`')
        elif arg == '2h':
            await message.channel.send('`' + tabulate(df2h, headers='keys', tablefmt='simple') + '`')
        else:
            result = df1.loc[df1['itemtype'] == arg]
            await message.channel.send('`' + tabulate(result, headers='keys', tablefmt='simple') + '`')  

這是我用來處理用戶輸入和操縱我的 dataframe 以向我提供用戶請求的信息的代碼塊。 我的問題是當有人輸入“:loot Leather”時,output 是:

itemname    itemtype    itemslot    stat1    stat2    source
----------  ----------  ----------  -------  -------  --------

但是當他們發送命令 ',loot Leather': 結果是這樣的:

    itemname                 itemtype    itemslot    stat1    stat2    source
--  -----------------------  ----------  ----------  -------  -------  ----------------------------
33  Cord of Anguished Cries  Leather     waist       haste    mastery  Dark Inquisitor Xanesh
36  Belt of Braided Vessels  Leather     waist       haste    vers     Il'gynoth, Corruption Reborn

由於某種原因,當行在 itemtype 列中以小寫皮革保存時,它不會返回結果。 請記住,這適用於 itemtype 列中的其他變量,例如“cloth”、“plate”、“mail”、“accessories”等。我可以將它們的輸入操縱成大寫第一個字母,但我覺得這是一個創可貼的解決方案。

有什么想法嗎? 我對 python 很陌生,總經驗不到 1 個月,但這個問題讓我很困惑,我似乎無法從 /r/learnpython subreddit 和 python Z482BCDDC8FE53ED51E68C77A93A 獲得任何幫助。 任何幫助將不勝感激。

問題是 lstrip 刪除了您在字符串左側指定的所有字符。 'l' 是您指定的字符列表的一部分。 lstrip 接收一個字符列表,而不是您要刪除的特定字符串。 嘗試這個:

#import re
#This way we use a regular expression to make sure it matches perfectly
arg = re.sub('^!loot ','',message.content) 

而不是這部分代碼:

arg = message.content.lstrip('!loot ')

字符串方法.lstrip刪除提供給它的前導字符。 因此,在您的情況下,它會刪除所有! , l , o , t它所作用的字符串最左邊的字符。 這就是為什么要去除皮革l的原因。

要達到刪除 substring '!loot '所需的結果,請使用.replace(',loot ', '')

example = '.loot leather' example_stripped = example.lstrip(',loot ') print(example_stripped) # 'eather' example_replaced = example.replace('!loot ', '') print(example_replaced) # 'leather'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM