【发布时间】: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