How do you remove a column from a structured numpy array?

前端 未结 2 2087
我寻月下人不归
我寻月下人不归 2021-02-07 11:36

I have another basic question, that I haven\'t been able to find the answer for, but it seems like something that should be easy to do.

Ok, imagine you have a structured

2条回答
  •  我在风中等你
    2021-02-07 12:08

    Having googled my way here and learned what I needed to know from Warren's answer, I couldn't resist posting a more succinct version, with the added option to remove multiple fields efficiently in one go:

    def rmfield( a, *fieldnames_to_remove ):
        return a[ [ name for name in a.dtype.names if name not in fieldnames_to_remove ] ]
    

    Examples:

    a = rmfield(a, 'foo')
    a = rmfield(a, 'foo', 'bar')  # remove multiple fields at once
    

    Or if we're really going to golf it, the following is equivalent:

    rmfield=lambda a,*f:a[[n for n in a.dtype.names if n not in f]]
    

提交回复
热议问题