Python Pandas连接数据帧并重命名索引

时间:2017-12-11 18:40:31

标签: python pandas

我有三个数据帧,所有数据帧都有50列和一行。每个数据帧中使用相同的列名,并且单行始终索引为0.我正在尝试连接它们以便更容易查看和比较数据。

features = pd.concat([raw_features, fea_features, transformed_features], axis=0)

现在我想重命名行。我尝试过几件事情,包括:

features = pd.concat([raw_features, fea_features, transformed_features], axis=0).reindex(['Raw_pulltest', 'FEA', 'Transformed_pulltest'])

,它给出错误cannot reindex from a duplicate axis

features = pd.concat([raw_features, fea_features, transformed_features], axis=0).reset_index().reindex(['Raw_pulltest', 'FEA', 'Transformed_pulltest'])

它给了我想要的结构,除了所有值现在都是nan

请帮我重命名连接数据框的索引吗?

1 个答案:

答案 0 :(得分:3)

<site name="yoursite-Site" id="108"> <other things removed just to get to showing you the bindingInfo... <bindings> <binding protocol="http" bindingInformation="*:3486:localhost" /> </bindings> </site> 中使用keys参数:

试试这个:

pd.concat

示例:

pd.concat([raw_features, fea_features, transformed_features], 
          axis=0, keys=['Raw_pulltest', 'FEA', 'Transformed_pulltest'])\
  .reset_index(level=1, drop=True)

输出:

d1 = pd.DataFrame([[1,1,1]],index=[0])

d2 = pd.DataFrame([[2,2,2]],index=[0])

d3 = pd.DataFrame([[3,3,3]], index=[0])

pd.concat([d1,d2,d3],axis=0, keys=['d1','d2','d3']).reset_index(level=1, drop=True)