一对一( pandas )遍历数据帧 [英] Loop through dataframe one by one (pandas)

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

问题描述

假设我们有一个包含A,B和C列的数据框:

Let's say we have a dataframe with columns A, B and C:

df = pd.DataFrame(columns =('A','B','C'), index=range(1))

列包含三行数值:

0     A     B      C
1    2.1   1.8    1.6
2    2.01  1.81   1.58
3    1.9   1.84   1.52

如何从1到3循环遍历每一行执行if语句,包括添加一些变量:

How does one loop through every row from 1 to 3 and then execute an if statement including add some variables:

if B1 > 1.5
    calc_temp   = A1*10
    calc_temp01 = C1*-10
if B2 > 1.5 
    calc_temp   = A2*10
    calc_temp01 = C2*-10
if B3 >1.5
    calc_temp   = A3*10
    calc_temp01 = C3*-10

以上可能吗?它必须知道某种范围,即具有某种计数器的全范围数据集编号,是吗? if语句应指向该特定行。

Is above even possible? It has to know a range of some sorts i.e. full range dataset number with some kind of counter, yes? The if statement should refer to that specific row.

推荐答案

我认为您需要 iterrows

I think you need iterrows:

for i, row in df.iterrows():
    if row['B'] > 1.5:
        calc_temp   = row['A'] *10
        calc_temp01 = row['C'] *-10

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

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