获取以给定字符串开头的列中的唯一项

Chi*_*nti 0 python arrays sorting pandas

考虑具有唯一值的列:

df['something'].unique() =array(['aa','bb','a','c']).
Run Code Online (Sandbox Code Playgroud)

现在我想知道哪些项目以 a 开头。我的预期答案是

'aa','a'
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 6

我认为这是列表理解与过滤的最简单用法:

out = [x for x in df['something'].unique() if x.startswith('a')]
print (out)
['aa', 'a']
Run Code Online (Sandbox Code Playgroud)

对于熊猫解决方案,请使用:

s = pd.Series(df['something'].unique())
out = s[s.str.startswith('a')].tolist()
print (out)
['aa', 'a']
Run Code Online (Sandbox Code Playgroud)