[英]Comparing rows in a DataFrame
I have a DataFrame suppose say df
.我有一个 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)
It will look like:它看起来像:
a b c
0 0 0 6
1 0 3 7
2 1 4 8
3 2 5 9
Now i want to compare row[0]
with row[1]
for column a and post the greater value or some remark in a new column, say d
.现在我想比较 a 列的
row[0]
和row[1]
并在新列中发布更大的值或一些备注,比如d
。
I have tried using:我试过使用:
for i in range(len(df):
if (df.iloc[a][i] > df.iloc[a][i+1]):
df[d] = "Post the greater number"
I am getting an error:我收到一个错误:
IndexError: single positional indexer is out-of-bounds
Can some one help me?有人能帮我吗?
The problem is that you when i is at the last index, i+1 will be out of bounds.问题是当 i 位于最后一个索引时, i+1 将超出范围。
I think you might want to write:我想你可能想写:
for i in range(len(df) - 1):
if (df.iloc[a][i] > df.iloc[a][i+1]):
df[d] = "Post the greater number"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.