Basically, I have a query returning a dataframe and row by row I want to generate new queries using the elements of the row as arguments for the next query -- the example walks through a simplified version and understanding that should be sufficient!
>>> import pandas as pd
>>> df2 = pd.DataFrame({'a' : ['colorado', 'california', 'texas', 'oregon'], 'b' : ['go buffs', 'go bears', 'go sooners', 'go ducks'], 'c' : [14,14,15,13]})
>>> df2
a b c
0 colorado go buffs 14
1 california go bears 14
2 texas go sooners 15
3 oregon go ducks 13
#Print element by element in column
>>> for x in df2['a']:
... print x
...
colorado
california
texas
oregon
#What I want is to print full ROWS
>>> df3 = df2.loc[:, ['a','b']]
>>> df3
a b
0 colorado go buffs
1 california go bears
2 texas go sooners
3 oregon go ducks
#I want to get something like
#for x, y in df3['a','b']:
# print 'in %s say %s!'%(x,y)
#and get:
#in colorado say go buffs!
#in california....etc
#How do I do that!?
You can unroll the tuples as you iterate through the dataframe and then just print the columns you desire:
for row in df2.itertuples():
index, a, b, c = row
print 'in %s say %s!'%(a,b)
in colorado say go buffs!
in california say go bears!
in texas say go sooners!
in oregon say go ducks!
Alternatively you can use iterrows
which as @DSM pointed out returns an index with nested values (actually a Series):
for row in df2.iterrows():
index, data = row
print 'in %s say %s!' % (data['a'], data['b'])
This will also output the same as the first code snippet
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With