【问题标题】:Get unique items in a column starting with a given string获取以给定字符串开头的列中的唯一项
【发布时间】:2020-06-25 10:49:33
【问题描述】:

考虑具有唯一值的列:

df['something'].unique() =array(['aa','bb','a','c']).

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

'aa','a'

【问题讨论】:

    标签: python arrays pandas sorting


    【解决方案1】:

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

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

    对于熊猫解决方案使用:

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

    【讨论】:

      猜你喜欢
      • 2010-10-05
      • 2018-01-31
      • 2016-12-04
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多