如何在 pandas 中选择不以某些 str 开头的行? [英] How to select rows that do not start with some str in pandas?
本文介绍了如何在 pandas 中选择不以某些 str 开头的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想选择值不以某些 str 开头的行.例如,我有一个pandas df
,我想选择不以t
和c
开头的数据.在此示例中,输出应为 mext1
和 okl1
.
I want to select rows that the values do not start with some str. For example, I have a pandas df
, and I want to select data do not start with t
, and c
. In this sample, the output should be mext1
and okl1
.
import pandas as pd
df=pd.DataFrame({'col':['text1','mext1','cext1','okl1']})
df
col
0 text1
1 mext1
2 cext1
3 okl1
我想要这个:
col
0 mext1
1 okl1
推荐答案
您可以使用 str 访问器来获取字符串功能.get
方法可以获取字符串的给定索引.
You can use the str accessor to get string functionality. The get
method can grab a given index of the string.
df[~df.col.str.get(0).isin(['t', 'c'])]
col
1 mext1
3 okl1
看起来您也可以将 startswith
与要排除的值的元组(而不是列表)一起使用.
Looks like you can use startswith
as well with a tuple (and not a list) of the values you want to exclude.
df[~df.col.str.startswith(('t', 'c'))]
这篇关于如何在 pandas 中选择不以某些 str 开头的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文