Dataframe未使用.loc返回列中1个字符串值的结果

2024-05-20 01:06:52 发布

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

我目前正在为一个webapp开发一个discord机器人,我计划将来使用Pandas制作一个数据框,存储来自WoW实例的所有可能的数据滴。我创建此bot是为了获取用户输入,例如“!loot cloth”,将“cloth”存储为参数,并将其传递给.loc函数,以便在“itemtype”列中搜索“cloth”。我遇到了一个有趣的bug,如果我搜索“leather”,它将不起作用

这是我的数据框中皮革部分的一个示例:

    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') + '`')  

这是我用来处理用户输入和操纵数据帧以提供用户请求的信息的代码块。我的问题是当有人输入“!“掠夺皮革”输出为:

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

但是当他们发出命令的时候!“掠夺皮革”,结果是:

    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 discord获得任何帮助。任何帮助都将不胜感激


Tags: ofthe数据用户messagearghasteleather
2条回答

问题是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删除提供给它的前导字符。因此,在您的例子中,它从它所作用的字符串的最左侧删除所有!lot字符。这就是为什么皮革被去除的原因

要获得删除子字符串'!loot '的预期结果,请使用.replace('!loot ', '')

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

相关问题 更多 >