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. 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.
iterrows
not do what you want?iterrows
does do that, you just have to use the second element of each tuple that it yields. (And you need to dof['a']
, notf[a]
. There's no way thatf[a]
can work.)