【问题标题】:Adding a list as row and column indices to pandas dataframe将列表作为行和列索引添加到熊猫数据框
【发布时间】:2019-01-02 10:58:25
【问题描述】:

我有一个熊猫数据框:

    0   1   2   3   4
0   4.8 2.1 0  6.2  0
1   8.5 4.9 0  2.2  0
2   0   5.3 6  9.3  0

还有两个列表: ind=[ind1,ind2,ind3]col=[col1,col2,col3,col4,col5]

我想重命名数据框索引和列,以便:

     col1 col2 col3 col4 col5
ind1 4.8  2.1   0    6.2  0
ind2 8.5  4.9   0    2.2  0
ind3  0   5.3   6    9.3  0

我尝试将列表转换为数据框并附加df.append(col)df.append(ind)。但这不起作用(可能是因为 df 的索引与 col 和 ind 数据帧的索引不同)

我该怎么做?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    set_axis

    df.set_axis(ind, inplace=False).set_axis(col, axis=1, inplace=False)
    
          col1  col2  col3  col4  col5
    ind1   4.8   2.1     0   6.2     0
    ind2   8.5   4.9     0   2.2     0
    ind3   0.0   5.3     6   9.3     0
    

    当前版本的 Pandas 需要使用 inplace=False。未来版本将默认使用inplace=False。这会将代码减少到

    df.set_axis(ind).set_axis(col, axis=1)
    

    这将并行使用set_indexset_indexset_axis 用于 axis=0 的主要区别在于 set_index 需要 Numpy 数组,而 set_axis 使用列表。 set_index使用列表,因为它将列表解释为列的列表,将其值用作 MultiIndex 中的元素。


    我喜欢user3483203rename 的使用。您还可以将callables 传递给参数。这是处理向索引和列添加通用前缀的便捷方法

    def prefix(t):
        def p(x):
            return f"{t}{int(x)+1}"
        return p
    
    df.rename(index=prefix('ind'), columns=prefix('col'))
    
          col1  col2  col3  col4  col5
    ind1   4.8   2.1     0   6.2     0
    ind2   8.5   4.9     0   2.2     0
    ind3   0.0   5.3     6   9.3     0
    

    【讨论】:

    • 非常好的更新,不知道你可以通过 callables!
    【解决方案2】:

    重新创建你的 df

    ind = ['ind1', 'ind2', 'ind3']  
    col = ['col1', 'col2', 'col3', 'col4', 'col5']
    pd.DataFrame(df.values,columns=col,index=ind)
    Out[377]: 
          col1  col2  col3  col4  col5
    ind1   4.8   2.1   0.0   6.2   0.0
    ind2   8.5   4.9   0.0   2.2   0.0
    ind3   0.0   5.3   6.0   9.3   0.0
    

    【讨论】:

      【解决方案3】:

      如果您想避免创建初始列表,请使用 renamef-strings

      df.rename(
          index={i: f'ind{i+1}' for i in df.index},
          columns={i: f'col{int(i)+1}' for i in df.columns}
      )
      
            col1  col2  col3  col4  col5
      ind1   4.8   2.1     0   6.2     0
      ind2   8.5   4.9     0   2.2     0
      ind3   0.0   5.3     6   9.3     0
      

      如果这只是一个示例命名约定并且您正在遵循另一种模式,我建议使用@piRSquared's 答案。

      【讨论】:

      • 有趣的方法:-)
      • 我觉得这个更干净df.rename(index=dict(zip(df.index, ind)), columns=dict(zip(df.columns, col)))
      • 哦,我同意如果他们仍在使用他们的列表,我试图提出一个替代方案,避免像他们一样创建初始列表。如果这些列表只是其他内容的占位符,则此方法没有帮助
      • 是的,我在评论之后意识到了这一点。事实上,你启发了我为我的答案添加一个替代方案。
      【解决方案4】:
      df = df.set_index(ind)
      
      df.columns = col
      

      【讨论】:

      • 用这样的列表设置索引是行不通的,至少在我的版本中是这样的......但是,你可以这样做df.set_index(pd.Series(ind))
      • 这至少在我的版本中有效,不确定你的版本是什么
      【解决方案5】:

      尝试:

      df.index = ind
      
      df.columns = col
      
      >>> df
            col1  col2  col3  col4  col5
      ind1   4.8   2.1     0   6.2     0
      ind2   8.5   4.9     0   2.2     0
      ind3   0.0   5.3     6   9.3     0
      

      【讨论】:

      • 这对我不起作用...这最终会导致整个数据帧的 NaNing
      • 有趣。你使用的是什么版本的pandas?我在0.21.1
      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多