如何遍历 pandas 数据框 [英] How to loop through a Pandas dataframe

查看:71
本文介绍了如何遍历 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历Pandas数据框.列表L包含用于指定XY应该从哪个行开始的值,即(1 :, 2 :, 3 ::).

I am trying to loop through a Pandas dataframe. The list L includes values that are used to specify what row X or Y should begin from i.e., (1:, 2:, 3:).

list = [1,2,3]

for L in list:
    X = data.ix[L:, 'X':]
    Y = data.ix[L:, 'Y']     
    regressor = LinearRegression()
    regressor.fit(X, Y)
    prediction = regressor.predict([[Variable]])

尝试上述操作时的错误是:

The error when attempting the above is:

TypeError: 'type' object is not iterable

推荐答案

IIUC,您可以执行以下操作:

IIUC you can do something like the following:

l = [1,2,3]
results = []

for idx in l:
    X = data.ix[idx:, 'X':]
    Y = data.ix[idx:, 'Y']     
    regressor = LinearRegression()
    regressor.fit(X, Y)
    results.append(regressor.predict([[Variable]]))

但是,我不知道这里的Variable是什么,您也可以执行以下操作:

However, I don't know what Variable is here, you could also just do the following:

for df in data.iloc[::1]:
    regressor = LinearRegression()
    regressor.fit(df['X'], df['Y'])
    results.append(regressor.predict([[Variable]]))

这篇关于如何遍历 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆