【问题标题】:DataFrames columns re-arrange based on list - DataFrames have different columnsDataFrame 列基于列表重新排列 - DataFrame 具有不同的列
【发布时间】:2021-05-19 15:15:06
【问题描述】:

我的问题总结:

  • 我有很多 DataFrame,所有的列都有相同的池(7 列,例如 COLUMN1:COLUMN7),但有时缺少一个或多个列(即 DataFrame 可能有 COLUMN1:COLUMN3 + COLUMN6:COLUMN7,因此是第 4 列和第 5 列缺失)。

  • 每个 DataFrame 每次都有以不同顺序排列的列(即 df1 有它的顺序,df2 有另一个顺序,df3 有另一个顺序等等......)。

  • 我想根据列表排列每个 DataFrame 中的列 用作基准的列(在本例中为列的列表 从 1 到 7)。

  • 期望的结果是所有数据框都具有相同的 基于此列表的列顺序,如果列缺少顺序 应保留(即,如果第 4 列和第 5 列缺失,则 列应为:COL1、COL2、COL3、COL6、COL7)。

更详细的说明:

我的代码中有几个通过清理一些数据集生成的 DataFrame。这些 DataFrame 中的每一个都有不同的列数和不同的顺序,但列限于此列表:'id', 'title', 'type', 'category', 'secondary category', 'date', 'description'。因此,此列表中的列最多为 7 个。示例:

DataFrame1 'id', 'title', 'date', 'category', 'type', 'description', 'secondary category'

DataFrame2 'id', 'description', 'title', 'type', 'category', 'date'

DataFrame3 'id', 'category', 'description', 'title'

期望的输出:

我想根据初始列表'id', 'title', 'type', 'category', 'secondary category', 'date', 'description' 对列进行排序,即使列数不同。 从上面的例子中,DataFrames 应该变成:

DataFrame1 'id', 'title', 'type', 'category', 'secondary category', 'date', 'description'

DataFrame2 'id', 'title', 'type', 'category', 'date', 'description'

DataFrame3 'id', 'title', 'category', 'description'

有没有办法,例如循环,以这种方式排列列?

【问题讨论】:

    标签: python pandas dataframe columnsorting


    【解决方案1】:

    您可以使用列表推导对列的顺序进行排序,并使用reindex 设置正确的顺序:

    desired_order = ['id', 'title', 'type', 'category', 'secondary category', 'date', 'description']
    
    df = df.reindex([i for i in desired_order if i in df.columns], axis=1)
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2021-12-30
      • 2021-07-12
      相关资源
      最近更新 更多