如果字符串以 pandas 中的某些字符开头,则选择行 [英] Select rows if string begins with certain characters in pandas
本文介绍了如果字符串以 pandas 中的某些字符开头,则选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 csv 文件作为下面的给定图片
I have a csv file as the given picture bellow
我正在尝试查找以字母 A 和 G 开头的任何单词或我想要的任何列表
I'm trying to find any word that will start with letter A and G or any list that I want
但我的代码返回错误任何想法我做错了什么?这是我的代码
but my code returns an error any Ideas what I'm doing wrong ? this is my code
if len(sys.argv) == 1:
print("please provide a CSV file to analys")
else:
fileinput = sys.argv[1]
wdata = pd.read_csv(fileinput)
print( list(filter(startswith("a","g"), wdata)) )
推荐答案
使用 Series.str.startswith
将列表转换为元组并通过 DataFrame.loc
和 布尔索引
:
wdata = pd.DataFrame({'words':['what','and','how','good','yes']})
L = ['a','g']
s = wdata.loc[wdata['words'].str.startswith(tuple(L)), 'words']
print (s)
1 and
3 good
Name: words, dtype: object
这篇关于如果字符串以 pandas 中的某些字符开头,则选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文