pandas :自定义类作为具有多索引的列标题 [英] Pandas: custom class as column header with multi indexing

查看:56
本文介绍了 pandas :自定义类作为具有多索引的列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对象用作多索引中的列标题 数据框,但我似乎无法正常工作. __eq__, __hash____str__仅适用于简单数据帧.

I'm trying to use objects as column headers in a multi indexed dataframe but I can't seem to get it to work. __eq__, __hash__ and __str__ only work for simple data frames.

这是一个小例子:

class Signal:

    def __init__(self, name):
        self.name = name


    def __eq__(self, other):
        try:
            return self.name == other or self.name == other.name
        except AttributeError as err:
            return False

    def __str__(self):
        return str(self.name)

    def __hash__(self):
        return hash(self.name)

if __name__ == '__main__':
    import pandas as pd
    import numpy as np
    a = Signal('name')
    b = Signal('name2')
    c = Signal('something')

    data = {
        ('A', a): np.arange(2),
        ('A', b): np.ones(2),
        ('B', c): np.zeros(2)
    }

    df = pd.DataFrame(data)

    print(df)
    print('-----------')
    print(df['A'])

我也尝试实现__le__, __ge____ne__.那没有 虽然做任何事情.我真的不知道我还能做什么. 有人有主意吗?

I also tried implementing __le__, __ge__ and __ne__. That did not do anything though. I don't really have a clue what else I could do. Anybody got some ideas?

推荐答案

在定义__lt____gt__

class Signal:

    def __init__(self, name):
        self.name = name


    def __eq__(self, other):
        try:
            return self.name == other or self.name == other.name
        except AttributeError as err:
            return False

    def __lt__(self, other):
        return self.name < other.name

    def __gt__(self, other):
        return self.name > other.name

    def __str__(self):
        return str(self.name)

    def __hash__(self):
        return hash(self.name)


import pandas as pd
import numpy as np
a = Signal('name')
b = Signal('name2')
c = Signal('something')

data = {
    ('A', a): np.arange(2),
    ('A', b): np.ones(2),
    ('B', c): np.zeros(2)
}

df = pd.DataFrame(data)

print(df, df['A'], sep='\n\n')

     A               B
  name name2 something
0    0   1.0       0.0
1    1   1.0       0.0

   name  name2
0     0    1.0
1     1    1.0

这篇关于 pandas :自定义类作为具有多索引的列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆