Python Pandas:创建等级列,移动原始列的最大等级

时间:2019-04-29 18:41:06

标签: python pandas rank

我需要能够
1.计算所有行中每一列的排名,
2.找到每行的最大列标签,
3.然后在每一行中移动原始df的最大排名列。

仅处理原始df中的数据时,这很简单。但是,如果需要不同的排名调用,似乎很难完成。

下面是我的Python Pandas代码来完成此任务。但这行不通。它似乎并没有像我期望的那样解释我的声明{ materials: [ { name: 'Mars_Atmosphere_Mat', parameters: [ { baseColorFactor: [1,1,1,1,], }, { baseColor: 'Mars_Atmosphere_Mat_baseColor', }, { diffuseColorFactor: null, }, { diffuseColor: null, }, { normal: null, }, { metallicFactor: 1, }, { roughnessFactor: 1, }, { specularFactor: null, }, { glossinessFactor: null, }, { specularGlossiness: null, }, { specularGlossinessCalculation: null, }, { metallicRoughness: null, }, { occlusion: null, }, { emissiveFactor: [0,0,0,1,], }, { emissive: null, }, { opacity: null, }, ], source: 'build/sceneform_sdk/default_materials/gltf_material.sfm', }, { name: 'Mars_mat', parameters: [ { baseColorFactor: [1,1,1,1,], }, { baseColor: 'Mars_mat_baseColor', }, { diffuseColorFactor: null, }, { diffuseColor: null, }, { normal: 'Mars_mat_normal', }, { metallicFactor: 0.33000000000000002, }, { roughnessFactor: 0.85999999999999999, }, { specularFactor: null, }, { glossinessFactor: null, }, { specularGlossiness: null, }, { specularGlossinessCalculation: null, }, { metallicRoughness: null, }, { occlusion: null, }, { emissiveFactor: [0,0,0,1,], }, { emissive: null, }, { opacity: null, }, ], source: 'build/sceneform_sdk/default_materials/gltf_material.sfm', }, ], model: { attributes: [ 'Position', 'TexCoord', 'Orientation', ], collision: { skin_width: 0.75, }, file: 'sampledata/models/Mars/Mars.gltf', name: 'Mars', recenter: true, scale: 0.5, }, samplers: [ { file: 'sampledata/models/Mars/Mars_mat_baseColor.png', name: 'Mars_mat_baseColor', pipeline_name: 'Mars_mat_baseColor.png', }, { file: 'sampledata/models/Mars/Mars_mat_normal.png', name: 'Mars_mat_normal', params: { usage_type: 'Normal', }, pipeline_name: 'Mars_mat_normal.png', }, { file: 'sampledata/models/Mars/Mars_Atmosphere_Mat_baseColor.png', name: 'Mars_Atmosphere_Mat_baseColor', pipeline_name: 'Mars_Atmosphere_Mat_baseColor.png', }, ], version: '0.54:1', } 。提出的建议将不胜感激。

df1['maxV'] = df1[df1['maxR']]

1 个答案:

答案 0 :(得分:0)

迭代行并将值累加到一个数组中:

maxVals = [np.nan]*3

for index, row in df1[pd.notna(df1['maxR'])].iterrows():   
      maxVals.append(df1.loc[index, row['maxR']])

df1['maxV'] = maxVals

替代方法:一种不太直观的方法可能是使用索引和值来对df1进行索引,这将返回更宽的数据框(#列等于#行的行数),对角线处有最大值:

maxVals = [np.nan]*3
newDf = df1.loc[df1['maxR'][3:].index, df1['maxR'][3:].values]
maxVals.extend(np.diag(newDf))
df1['maxV'] = maxVals