遍历熊猫数据框

时间:2019-02-24 18:44:27

标签: python-3.x pandas

我的变量noExperience1是一个数据框 我正在尝试通过以下循环:

num = 0

for row in noExperience1:
 if noExperience1[row+1] - noExperience1[row] > num:
    num = noExperience1[row+1] - noExperience1[row]

print(num)

我的目标是找到从一个x值到另一个x值的y值最大的不同。但是我得到一个错误,即我的if语句的行必须是字符串而不是整数。我该如何解决这个问题,以便获得号码?

1 个答案:

答案 0 :(得分:0)

我们无法使用索引直接访问一行数据框。我们需要使用loc或iloc。我刚刚解决了您提出的问题。

`noExperience1=pd.read_csv("../input/data.csv")#reading CSV file
num=0
for row in range(1,len(noExperience1)): #iterating row in all rows of DF
    if int(noExperience1.loc[row]-noExperience1.loc[row-1]) > num:
        num = int(noExperience1.loc[row]-noExperience1.loc[row-1])
print(num)`

注意: 1.Column Slicing:DataFrame [ColName] ==>将为您提供指定列的所有输入。 2.Row Slicing:DataFrame.loc [RowNumber] ==>将为您提供指定行numbe的完整行。RowNumber以0开头。

希望这会有所帮助。