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)
我认为这是列表理解与过滤的最简单用法:
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)
归档时间: |
|
查看次数: |
82 次 |
最近记录: |