python - 比较 DataFrame 中的行
问题描述
我有一个 DataFrame 假设说df
。
import pandas as pd
data_dic = {
"a": [0,0,1,2],
"b": [0,3,4,5],
"c": [6,7,8,9]
}
df = pd.DataFrame(data_dic)
它看起来像:
a b c
0 0 0 6
1 0 3 7
2 1 4 8
3 2 5 9
现在我想row[0]
与a 列进行比较,row[1]
并在新列中发布更大的值或一些评论,比如说d
。
我试过使用:
for i in range(len(df):
if (df.iloc[a][i] > df.iloc[a][i+1]):
df[d] = "Post the greater number"
我收到一个错误:
IndexError: single positional indexer is out-of-bounds
有人能帮我吗?
解决方案
问题是当 i 位于最后一个索引时, i+1 将超出范围。
我想你可能想写:
for i in range(len(df) - 1):
if (df.iloc[a][i] > df.iloc[a][i+1]):
df[d] = "Post the greater number"
推荐阅读
- javascript - 使用 ng-if 或 ng-show 根据用户输入显示部分
- r - 根据数据框列条件更新 For 循环外的向量
- scala - 如何从 Kotlin 调用 Scala 密封特征?
- python - 访问 xarray DataArray 中的单个元素
- html - 设置 nativeElement.scrollTop 不起作用,滚动到 div 的底部
- php - 如何使用 codeigniter 进行日期之间的查询?
- rdf - 无法获取输出 (OWL) 文件中的所有值
- javascript - 如何使用 ytdl-core 通过 Discord.js 机器人发送下载的 .mp3?
- spring - 带有 Kotlin 的 Spring Boot - @Value 注释未按预期工作
- c# - 如何从 WindowsService 获取当前用户配置文件位置