小编Pys*_*313的帖子

Pandas 类与 Pandas .p​​ipe()

@pd.api.extensions.register_dataframe_accessor("data_cleaner")
class DataCleaner:

    def __init__(self, pandas_obj):
        self._obj = pandas_obj

    def multiply(self, col):
        self._obj[col] = self._obj[col] * self._obj[col]
        return self._obj 
    
    def square(self, col):
        self._obj[col] = self._obj[col]**2
        return self._obj 
    
    def add_strings(self, col):
        self._obj[col] = self._obj[col] + self._obj[col]
        return self._obj 

    def process_all(self):
        self._obj.pipe(
            self.multiply(col='A'),
            self.square(col='B')
            self.add_strings(col='C')
        )
    

class DataProcessor(DataCleaner):

    data = [
        [1, 1.5, "AABB"],
        [2, 2.5, "BBCC"],
        [3, 3.5, "CCDD"],
        [4, 4.5, "DDEE"],
        [5, 5.5, "EEFF"],
        [6, 6.5, "FFGG"],
    ]

    def __init__(self):
        self.df = pd.DataFrame(data=DataProcessor.data, columns=['A', 'B', 'C'])

    def get_data(self):
        return self.df …
Run Code Online (Sandbox Code Playgroud)

python accessor dataframe pandas

5
推荐指数
1
解决办法
1060
查看次数

标签 统计

accessor ×1

dataframe ×1

pandas ×1

python ×1