python - 按字符串列中最后 3 个字符选择行

标签 python pandas

我有这个数据框

      name               year ...
0     Carlos - xyz       2019
1     Marcos - yws       2031
3     Fran - xxz         2431
4     Matt - yre         1985
...

我想创建一个名为 type 的新列。 如果人名以“xyz”或“xxz”结尾,我希望类型为“big”

所以,它应该看起来像这样:

      name               year   type
0     Carlos - xyz       2019    big
1     Marcos - yws       2031  
3     Fran - xxz         2431    big
4     Matt - yre         1985
...

有什么建议吗?

最佳答案

选项 1
使用 str.contains 生成掩码:

m = df.name.str.contains(r'x[yx]z$')

或者,

sub_str = ['xyz', 'xxz']
m = df.name.str.contains(r'{}$'.format('|'.join(sub_str)))

现在,您可以使用 np.where 创建列,

df['type'] = np.where(m, 'big', '')

或者,loc 代替 np.where

df['type'] = ''
df.loc[m, 'type'] = 'big'

df
           name  year type
0  Carlos - xyz  2019  big
1  Marcos - yws  2031     
3    Fran - xxz  2431  big
4    Matt - yre  1985     

选项 2
作为替代方案,请考虑 str.endswith + np.ological_or.reduce

sub_str = ['xyz', 'xxz']
m = np.logical_or.reduce([df.name.str.endswith(s) for s in sub_str])

df['type'] = ''
df.loc[m, 'type'] = 'big'

df
           name  year type
0  Carlos - xyz  2019  big
1  Marcos - yws  2031     
3    Fran - xxz  2431  big
4    Matt - yre  1985    

关于python - 按字符串列中最后 3 个字符选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49524245/

相关文章:

python - 在类体内使用非局部或全局

使用管道的Python多进程非阻塞相互通信

python - Plotly python 气泡图 - 添加文本

python - Pandas If 函数或 groupby

python - Pandas HDFStore 从嵌套列中选择

python - 使用 unittest 框架测试 pandas 数据框

Python 闭包,默认参数不等于使用 functools.partial 的解决方案?

python - matplotlib 光标信息似乎依赖于刻度分辨率 - 如何更改此依赖关系

python - 解包单 channel 波形数据并将其存储在数组中

python - 如何计算两个相似的 pandas 列之间的索引交集?