How to reorder columns on a subclassed pandas Dataframe

huangapple go评论92阅读模式
英文:

How to reorder columns on a subclassed pandas Dataframe

问题

我想重新排序一个子类化的pandas dataframe的列。

我从这个问题中了解到可能有一种更好的方法来避免子类化一个dataframe,但我仍然想知道如何处理这个问题。

如果不使用子类化,我会按照传统的方式来做:

import pandas as pd

data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
df = pd.DataFrame(data)

df = df[['Symbol', 'Name', 'Description']]

但是使用子类化的方式,保持与传统方式相同的行为并不会重新排序列:

import pandas as pd

class SubDataFrame(pd.DataFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self = self._reorder_columns()
    
    def _reorder_columns(self):
        first_columns = ['Symbol', 'Name', 'Description']
        return self[first_columns + [c for c in self.columns if c not in first_columns]]
    
data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
df = SubDataFrame(data)

我相信我的错误在于重新分配self,这没有任何效果。

如何在子类化的dataframe上实现列重新排序?

英文:

I want to reorder dataframe columns from a subclassed pandas dataframe.

I understood from this question there might be a better way for not subclassing a dataframe, but I'm still wondering how to approach this.

Without subclassing, I would do it in a classic way:

import pandas as pd

data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
df = pd.DataFrame(data)

df = df[['Symbol', 'Name', 'Description']]

But with subclassing, keeping the same behavior as the classic one doesn't reorder the columns:

import pandas as pd

class SubDataFrame(pd.DataFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self = self._reorder_columns()
    
    def _reorder_columns(self):
        first_columns = ['Symbol', 'Name', 'Description']
        return self[first_columns + [c for c in self.columns if c not in first_columns]]
    
data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
df = SubDataFrame(data)

I believe my mistake is in reassigning self which doesn't have any effect.

How can I achieve column reordering on the subclassed dataframe?

答案1

得分: 1

Pandas中具有inplace参数的方法使用了私有方法_update_inplace。你可以做同样的事情,但是请确保跟进未来Pandas的开发,以防此方法发生变化:

import pandas as pd

class SubDataFrame(pd.DataFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._update_inplace(self._reorder_columns())
    
    def _reorder_columns(self):
        first_columns = ['Symbol', 'Name', 'Description']
        return self[first_columns + [c for c in self.columns if c not in first_columns]]
    
data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
df = SubDataFrame(data)

输出结果:

     Symbol    Name Description
0  mysymbol  myname      mydesc
英文:

Pandas methods that have an inplace parameter use the private method _update_inplace. You could do the same, but be sure to follow future pandas development in case this method changes:

import pandas as pd

class SubDataFrame(pd.DataFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._update_inplace(self._reorder_columns())
    
    def _reorder_columns(self):
        first_columns = ['Symbol', 'Name', 'Description']
        return self[first_columns + [c for c in self.columns if c not in first_columns]]
    
data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
df = SubDataFrame(data)

Output:

     Symbol    Name Description
0  mysymbol  myname      mydesc

huangapple
  • 本文由 发表于 2023年8月9日 16:55:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定