如何在一个子类化的pandas DataFrame上重新排列列。

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

How to reorder columns on a subclassed pandas Dataframe

问题

我想重新排列子类化的pandas数据框中的列。

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

如果不子类化,我会采用经典的方式来做:

  1. import pandas as pd
  2. data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
  3. df = pd.DataFrame(data)
  4. df = df[['Symbol', 'Name', 'Description']]

但是在子类化的情况下,保持与经典方式相同的行为不会重新排列列:

  1. import pandas as pd
  2. class SubDataFrame(pd.DataFrame):
  3. def __init__(self, *args, **kwargs):
  4. super().__init__(*args, **kwargs)
  5. self = self._reorder_columns()
  6. def _reorder_columns(self):
  7. first_columns = ['Symbol', 'Name', 'Description']
  8. return self[first_columns + [c for c in self.columns if c not in first_columns]]
  9. data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
  10. df = SubDataFrame(data)

我相信我的错误在于重新分配self,这不会产生任何效果。

如何在子类化的数据框上实现列重新排列?
1: https://stackoverflow.com/a/35619846/3010217

英文:

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:

  1. import pandas as pd
  2. data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
  3. df = pd.DataFrame(data)
  4. df = df[['Symbol', 'Name', 'Description']]

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

  1. import pandas as pd
  2. class SubDataFrame(pd.DataFrame):
  3. def __init__(self, *args, **kwargs):
  4. super().__init__(*args, **kwargs)
  5. self = self._reorder_columns()
  6. def _reorder_columns(self):
  7. first_columns = ['Symbol', 'Name', 'Description']
  8. return self[first_columns + [c for c in self.columns if c not in first_columns]]
  9. data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
  10. 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的发展以防此方法发生更改:

  1. import pandas as pd
  2. class SubDataFrame(pd.DataFrame):
  3. def __init__(self, *args, **kwargs):
  4. super().__init__(*args, **kwargs)
  5. self._update_inplace(self._reorder_columns())
  6. def _reorder_columns(self):
  7. first_columns = ['Symbol', 'Name', 'Description']
  8. return self[first_columns + [c for c in self.columns if c not in first_columns]]
  9. data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
  10. df = SubDataFrame(data)

输出:

  1. Symbol Name Description
  2. 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:

  1. import pandas as pd
  2. class SubDataFrame(pd.DataFrame):
  3. def __init__(self, *args, **kwargs):
  4. super().__init__(*args, **kwargs)
  5. self._update_inplace(self._reorder_columns())
  6. def _reorder_columns(self):
  7. first_columns = ['Symbol', 'Name', 'Description']
  8. return self[first_columns + [c for c in self.columns if c not in first_columns]]
  9. data = {'Description':['mydesc'], 'Name':['myname'], 'Symbol':['mysymbol']}
  10. df = SubDataFrame(data)

Output:

  1. Symbol Name Description
  2. 0 mysymbol myname mydesc

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

发表评论

匿名网友

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

确定