如何遍历熊猫中的列标题和行值?

时间:2019-02-12 17:10:37

标签: python-3.x pandas dataframe

对于数据帧df,我想完成以下操作:(1)遍历列标头,并将其中带有“ Vol”的每个标头更改为Vol_Adj,以及(2)遍历与那些标头对应的值,然后将其除以标题为“ eTIV”的列中的对应值(每行)

这是我尝试过的:

for col in df.columns:
    if 'Vol' in col:
        df[col] = df.col / df.eTIV
        df= df.rename(index=str, columns={col: col + "_Adj"})

我遇到以下错误:AttributeError:'DataFrame'对象没有属性'col'。

分解代码时,它可以单独工作:

for col in df.columns:
    print(col)

这也是如此:

df['one'] = df.one / df.five

这样做:

df= df.rename(index=str, columns={"three": "three_Adj"})

但是这些命令没有集成在for循环中。

1 个答案:

答案 0 :(得分:0)

已解决:

for key,value in df.iteritems():
    if 'Vol' in key:
        key = key + '_Adj'
        value = value / df.five
        print(key,value)
相关问题