小编Mar*_*rkS的帖子

使用行名称、列名称和最大列值创建数据框

我正在浏览从 PACKT 购买的视频包来学习 pandas。作者使用 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', …
Run Code Online (Sandbox Code Playgroud)

dataframe python-3.x pandas

5
推荐指数
1
解决办法
2万
查看次数

按每个字典键对字典列表进行排序

我正在创建一个字典列表,然后我想按键的值从最低到最高对列表中的字典进行排序。

一切正常,除了排序:

def pythagorean(x1, y1, x2=0, y2=0):
    return ((x1 - x2)**2 + (y1 - y2)**2)**0.5


points = [(-2, -4), (0, -2), (-1, 0), (3, -5), (-2, -3), (3, 2)]

dicts = []

for coord in points:
    d = {}
    a1, b1 = coord
    distance = pythagorean(a1, b1)
    d[distance] = (a1, b1)
    dicts.append(d)

for i in dicts:
    print(i)

dist_list = []

for item in dicts:
    for key in item:
        dist_list.append(key)


temp = sorted(dicts, key=lambda d: [k in d for k in …
Run Code Online (Sandbox Code Playgroud)

python sorting dictionary list python-3.x

2
推荐指数
1
解决办法
5408
查看次数

使用 set() 解决“一去不复返”

我正在参加 Udemy 课程。我正在处理的问题是取两个字符串并确定它们是否彼此“一次编辑”。这意味着您可以对一个字符串进行一次更改——更改一个字母、添加一个字母、删除一个字母——并使其与另一个相同。

例子:

s1a = "abcde"
s1b = "abfde"

s2a = "abcde"
s2b = "abde"

s3a = "xyz"
s3b = "xyaz"
Run Code Online (Sandbox Code Playgroud)
  • s1a将 更改'c''f'
  • s2a删除'c'.
  • s3a添加'a'.

教师解决方案(和测试套件):

def is_one_away(s1, s2):
    if len(s1) - len(s2) >= 2 or len(s2) - len(s1) >= 2:
        return False
    elif len(s1) == len(s2):
        return is_one_away_same_length(s1, s2)
    elif len(s1) > len(s2):
        return is_one_away_diff_lengths(s1, s2)
    else:
        return is_one_away_diff_lengths(s2, s1)


def is_one_away_same_length(s1, s2):
    count_diff = 0
    for …
Run Code Online (Sandbox Code Playgroud)

python set

1
推荐指数
1
解决办法
564
查看次数

打印字典减去两个元素

Python 3.6

所有调试输出均来自PyCharm 2017.1.2

我有一个程序可以访问这部分代码:

if len(errdict) == 21:
    for k, v in errdict.items():
        if k == 'packets output' or 'bytes':
            continue
        print(k, v)
    print()
Run Code Online (Sandbox Code Playgroud)

执行时k:和errdict {}的值如下:

k={str}'input errors'

__len__ = {int} 21
'CRC' (73390624) = {int} 0
'babbles' (73390464) = {int} 0
'bytes' (73390496) = {int} 0
'collisions' (73455360) = {int} 0
'deferred' (73455440) = {int} 0
'frame' (73390592) = {int} 0
'ignored' (73390688) = {int} 0
'input errors' (73455280) = {int} 0
'input packets with dribble condition detected' …
Run Code Online (Sandbox Code Playgroud)

python dictionary

-1
推荐指数
1
解决办法
133
查看次数

使用iter协议的类

我有一个创建建筑的小班.我想更多地了解国际热核实验堆.以下作品:

class Building(object):
    def __init__(self, floors):
        self._floors = [None] * floors

    def __setitem__(self, floor_number, data):
        self._floors[floor_number] = data

    def __getitem__(self, floor_number):
        return self._floors[floor_number]

    def __iter__(self):
        for floor_number in self._floors:
            yield self._floors[floor_number]


building1 = Building(9)  # Construct a building with 9 floors
building1[0] = 'Reception'
building1[1] = 'ABC Corp'
building1[2] = 'DEF Inc'
building1[3] = 'Apple'
building1[4] = 'Cisco'
building1[5] = 'Microsoft'
building1[6] = 'Dell'
building1[7] = 'EMC'
building1[8] = 'HPE'

'''
for floor in building1:
    print(building1[floor])
'''

print(building1[6]) …
Run Code Online (Sandbox Code Playgroud)

python iterator python-3.6

-1
推荐指数
1
解决办法
38
查看次数