简体   繁体   English

大熊猫遍历每一行

[英]pandas iterating through each row

There have been no questions which addressed this specific point: 没有问题可以解决这一特定问题:

I want to iterate through the rows of a dataframe. 我想遍历数据框的行。 Specifically, within each row, I would like to call by column name. 具体来说,在每一行中,我想按列名进行调用。 Is there a way to do this? 有没有办法做到这一点? If so, please demonstrate. 如果是这样,请示范。

I am familiar with the df[<column_name>][<index_name>] , but I don't think this addresses things. 我熟悉df[<column_name>][<index_name>] ,但是我认为这不能解决问题。 Perhaps I can mix this with the transpose function, but then I lose track of datatypes, right? 也许我可以将其与转置函数混合使用,但是随后我却无法掌握数据类型,对吗?

For example, if we have 例如,如果我们有

    a b c d
 i1 1 1 2 1
 i2 2 2 1 1

I want to be able to say: 我想能够说:

for f in some_iterator():
    print 'a is ' str(f['a'])
    print f['b'] + f['c']
    #skip f['d']

But as it stands, I can't depend on the column names, in this case, "a,b,c,d" to do this. 但就目前情况而言,我不能依靠列名来完成此操作,在这种情况下,请使用“ a,b,c,d”。

Let df be a pandas.DataFrame object. dfpandas.DataFrame对象。

We can iterate through the rows by: 我们可以通过以下方式遍历各行:

for row in df.iterrows():
    print str(row[0]) + " is the index of the row"
    print str(row[1]) + " is a series with rows having \
    labels the same as the columns of the dataframe"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM