python - 基于先前值和 Python 上的 ID 的重复总和?
问题描述
我想看看是否有一种方法可以在 Python 上计算类似以下内容,这可能吗?
ID Rotation Starting_degree Current_degree
1 40 360 320
1 55 320 265
2 70 360 290
1 15 265 250
2 20 290 270
3 30 360 330
3 60 330 270
1 25 250 225
一般来说,我的代码是df['current_degree'] = df.apply(lambda row: row.starting_degree - row.rotation, axis = 1)
,但我希望根据 ID 和任何先前的计算来更改起始度数。
每个新 ID 的起始度数重置为 360。
解决方案
IIUC,你想计算给定旋转的当前度数:
# assume that all IDs start with 360
df['Start'] = 360
# grouping by ID
groups = df.groupby("ID")
# compute the total rotation by cumsum
df['Rot_so_far'] = groups.Rotation.cumsum()
# current degree
df['Current_degree'] = df['Start'] - df['Rot_so_far']
您可能希望对非负电流度数进行 360 模运算。
推荐阅读
- javascript - 如何将 JSON 提取数据分离到不同的 div 中
- r - 按范围分组数据,而不是 R 中的所有数字数据
- angular - 如何使用带有外部 IdentityServer 的 OAuth2 保护 Angular 代码
- python - 将值和函数重新序列化到 Python 中的新环境中
- python - 在python中循环遍历数据框字典并将字典中的每个数据框与单个数据框合并
- docker - 发布容器端口时如何使用另一个容器的网络堆栈
- c# - C#计数计时器不会停止计数
- python - Netmiko OSError:在 send_command_expect:Destination 中从未检测到搜索模式
- ruby-on-rails - Rails Mailgun::CommunicationError 403 仅在生产中
- azure - 我如何知道 Azure VMSS 的特定实例上安装了哪个映像版本?