class myDataframe(pd.Dataframe)
def init.....
....
self.my_attribute = ['a', 'b', 'c']
如果这么干的话,pandas会提示UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
有咩有什么办法可以避免这个warning咧?
stackoverflow 上也有人问:
https://stackoverflow.com/questions/47631230/pandas-store-a-list-as-a-dataframe-attribute
谢谢
from pandas import DataFrame
class myDataframe(DataFrame):
my_attribute=['a','b','c']
def __init__(...)...
...
print(myDataframe.my_attribute)
d=myDataframe()
print(d.my_attribute)
# 输出结果为:
['a', 'b', 'c']
['a', 'b', 'c']
这种情况,你就要在子类中定义一个静态变量(静态属性),就可以通过子类的类名或者实例进行调用了,也就是在构造函数前添加属性。