【问题标题】:Create dataframe with row name, column name, and max column value使用行名、列名和最大列值创建数据框
【发布时间】:2019-04-20 16:56:50
【问题描述】:

我正在浏览从 PACKT 购买的视频包来学习熊猫。作者使用 jijna2 style() 突出显示每列中的最大值。我很快发现我不能在 PyCharm 中使用这种技术。所以我决定提取这些值。

我要做的是通过从具有 N 列的数据框中提取行索引、列名和最大列值来创建一个三列数据框,然后创建新的数据框。新的数据框将显示每一行(如果有关系则显示所有适当的行)、列和该列中的最大值。

我创建了一个玩具数据框来处理代码。

下面是我的代码以及输出,最底部是我真正希望新数据框看起来的样子。

我知道我正在使用打印语句。到目前为止,该代码是我唯一使用过的代码,如果我有平局的话,它可以正确选择多行。

我抓住整行,这是我不想要的。我也不确定如何从提取的数据中构建提议的新数据框。

import pandas as pd


raw_data = {
            'dogs': [42, 39, 86, 15, 23, 57, 68, 81, 86],
            'cats': [52, 41, 79, 80, 34, 47, 19, 22, 59],
            'sheep': [62, 37, 84, 51, 67, 32, 23, 89, 73],
            'lizards': [72, 43, 36, 26, 53, 88, 88, 34, 69],
            'birds': [82, 35, 77, 63, 18, 12, 45, 56, 58],
            }

df = pd.DataFrame(raw_data,
                  index=pd.Index(['row_1', 'row_2', 'row_3', 'row_4', 'row_5', 'row_6', 'row_7', 'row_8', 'row_9'], name='Rows'),
                  columns=pd.Index(['dogs', 'cats', 'sheep', 'lizards', 'birds'], name='animals'))

print(df)
print()

# Get a list of all columns names
cols = df.columns
print(cols)
print('*****')

for col in cols:
    print((df[df[col] == df[col].max()]))


'''
animals  dogs  cats  sheep  lizards  birds
Rows                                      
row_3      86    79     84       36     77
row_9      86    59     73       69     58
animals  dogs  cats  sheep  lizards  birds
Rows                                      
row_4      15    80     51       26     63
animals  dogs  cats  sheep  lizards  birds
Rows                                      
row_8      81    22     89       34     56
animals  dogs  cats  sheep  lizards  birds
Rows                                      
row_6      57    47     32       88     12
row_7      68    19     23       88     45
animals  dogs  cats  sheep  lizards  birds
Rows                                      
row_1      42    52     62       72     82
'''

row_3     dogs        86
row_9     dogs        86
row_4     cats        80
row_8     sheep       89
row_6     lizards     88
row_7     lizards     88
row_1     birds       82

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    您可以使用 .where 和 eq 来检查 mask 的 no max 值作为 NaN 然后 stack

    df.where(df.eq(df.max())).stack().sort_index(level=1).reset_index()
        Rows  animals     0
    0  row_3     dogs  86.0
    1  row_9     dogs  86.0
    2  row_4     cats  80.0
    3  row_8    sheep  89.0
    4  row_6  lizards  88.0
    5  row_7  lizards  88.0
    6  row_1    birds  82.0
    

    【讨论】:

    • @jezrael 不知道为什么会再次发生,我确实投票给你
    【解决方案2】:

    numpy.where 用于匹配maxes 的索引并通过索引创建新的DataFrame - 如果性能在大型DataFrame 中很重要,则更好:

    c, r = np.where(df.eq(df.max()).T)
    df = pd.DataFrame({'idx':df.index[r], 'cols':df.columns[c], 'vals': df.values[r, c]})
    print(df)
         idx     cols  vals
    0  row_3     dogs    86
    1  row_9     dogs    86
    2  row_4     cats    80
    3  row_8    sheep    89
    4  row_6  lizards    88
    5  row_7  lizards    88
    6  row_1    birds    82
    

    另一个唯一的带有DataFrame.unstackGroupBy.transform 的熊猫解决方案,用于按第一级比较每组的max 值:

    s = df.unstack()
    df = s[s.groupby(level=0).transform('max').eq(s)].reset_index(name='vals')
    print(df)
       animals   Rows  vals
    0     dogs  row_3    86
    1     dogs  row_9    86
    2     cats  row_4    80
    3    sheep  row_8    89
    4  lizards  row_6    88
    5  lizards  row_7    88
    6    birds  row_1    82
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多