在单元格中的特定位置获取元音的单词

2024-04-25 22:40:17 发布

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

我有一个熊猫数据帧,它有以下列 affix, word, sense and meaning。现在,如果我想获得列word中的所有条目,其最后一个字符的第四个字符是a。你知道吗

下面的片段为我提供了答案

pd[(pd['affix'] == 'man') & (pd['word'].str[-4] == 'a' )  ]

输出为

        affix   word        sense                  meaning
9900    man     cameraman   who     # somebody who operates a [[movie]] [[camera]]...
9901    man     cameraman   who     # {{l|en|cameraman}} {{gloss|somebody who oper...

但是,如果我想保存最后一个字符的第四个字符是元音的条目,那么下面的代码片段就不起作用了。任何帮助都有助于取得成果

  pd[(pd['affix'] == 'man') & (pd['word'].str[-4] in ['a','e','i','o','u'] )  ]

显示的错误是

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Tags: and数据答案条目字符wordpdwho
2条回答

可以与^{}匹配

pd[(pd['affix'] == 'man') & pd.str.match('.*[aeiou].{3}$')

'.*[aeiou].{3}$'是一个正则表达式,表示:

  • '.*'匹配任意次数
  • '[aeiou]'后跟括号之间列表中的单个字符
  • '.{3}$'后跟任意3个字符,然后后跟字符串的结尾。你知道吗

我想你需要^{}

pd[(pd['affix'] == 'man') & (pd['word'].str[-4].isin(['a','e','i','o','u']))]

相关问题 更多 >

    热门问题