pro*_*eak 5 python string pandas
我有一个 csv 文件作为给定的图片波纹管
我试图找到任何以字母 A 和 G 开头的单词或我想要的任何列表
但是我的代码返回错误任何想法我做错了什么?这是我的代码
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)) )
Run Code Online (Sandbox Code Playgroud)
要获取相关行,请提取第一个字母,然后使用isin:
df
words frequency
0 what 10
1 and 8
2 how 8
3 good 5
4 yes 7
df[df['words'].str[0].isin(['a', 'g'])]
words frequency
1 and 8
3 good 5
Run Code Online (Sandbox Code Playgroud)
如果您想要特定的列,请使用loc:
df.loc[df['words'].str[0].isin(['a', 'g']), 'words']
1 and
3 good
Name: words, dtype: object
df.loc[df['words'].str[0].isin(['a', 'g']), 'words'].tolist()
# ['and', 'good']
Run Code Online (Sandbox Code Playgroud)
使用Series.str.startswith与转换列表,元组和过滤DataFrame.loc用boolean indexing:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7284 次 |
| 最近记录: |