小编Dav*_*vid的帖子

在 pandas .loc[] 赋值中访问下一行、上一行或当前行

在 pandas 文档说明书的 if-then 部分,我们可以根据使用loc[].

 df = pd.DataFrame({'AAA' : [4,5,6,7], 
'BBB' : [10,20,30,40],
'CCC' : [100,50,-30,-50]})
#    AAA  BBB  CCC
# 0    4   10  100
# 1    5   20   50
# 2    6   30  -30
# 3    7   40  -50

df.loc[df.AAA >= 5,'BBB'] = -1
#    AAA  BBB  CCC
# 0    4   10  100
# 1    5   -1   50
# 2    6   -1  -30
# 3    7   -1  -50
Run Code Online (Sandbox Code Playgroud)

但是,如果我想使用 编写涉及前一行或后一行的条件.loc[]怎么办?例如,假设我想分配当前行和下一行df.BBB=5之间的差值大于或等于 50 …

python pandas

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

为什么 Datetime 的 `.timestamp()` 方法返回 `OSError: [Errno 22] Invalid argument`?

.timestamp()在代码中两次使用该函数,将日期时间对象转换为纪元时间。第一次调用.timestamp()看起来像这样:

import datetime    
origin_epoch = origin.timestamp()
Run Code Online (Sandbox Code Playgroud)

变量的内容originorigin_epoch有:

Visual Studio Code 的变量调试器的屏幕截图。

同时,如果我尝试在代码的其他地方调用相同的方法

import datetime
print(datetime.datetime(1900, 1, 1, 19, 6, 28).timestamp())
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误:OSError: [Errno 22] Invalid argument这是为什么?

编辑:此错误发生在 Windows 10 上。

python datetime epoch python-3.x

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

在Python中“就地”修改链表节点

我在练习 LeetCode 问题(诚然很简单)时遇到了我的问题。然而,我真正的问题是关于Python的,而不是问题本身的答案。您将在下面看到完整的问题陈述,然后我解释我的方法,将其与实际解决方案进行对比,然后(最后)提出我的问题。


LeetCode问题:删除链表中的节点

问题:

编写一个函数来删除单链表中的节点(尾部除外),并且只能访问该节点。

给定链表 -- head = [4,5,1,9],如下所示:

图像

示例1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
Run Code Online (Sandbox Code Playgroud)

示例2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function. …
Run Code Online (Sandbox Code Playgroud)

python linked-list singly-linked-list

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