I am trying to iterate over the rows of a Python Pandas dataframe. Within each row of the dataframe, I am trying to to refer to each value along a row by its column name.
Here is what I have:
JavaScript
x
17
17
1
import numpy as np
2
import pandas as pd
3
4
df = pd.DataFrame(np.random.rand(10,4),columns=list('ABCD'))
5
print df
6
A B C D
7
0 0.351741 0.186022 0.238705 0.081457
8
1 0.950817 0.665594 0.671151 0.730102
9
2 0.727996 0.442725 0.658816 0.003515
10
3 0.155604 0.567044 0.943466 0.666576
11
4 0.056922 0.751562 0.135624 0.597252
12
5 0.577770 0.995546 0.984923 0.123392
13
6 0.121061 0.490894 0.134702 0.358296
14
7 0.895856 0.617628 0.722529 0.794110
15
8 0.611006 0.328815 0.395859 0.507364
16
9 0.616169 0.527488 0.186614 0.278792
17
I used this approach to iterate, but it is only giving me part of the solution – after selecting a row in each iteration, how do I access row elements by their column name?
Here is what I am trying to do:
JavaScript
1
5
1
for row in df.iterrows():
2
print row.loc[0,'A']
3
print row.A
4
print row.index()
5
My understanding is that the row is a Pandas series. But I have no way to index into the Series.
Is it possible to use column names while simultaneously iterating over rows?
Advertisement
Answer
I also like itertuples()
JavaScript
1
4
1
for row in df.itertuples():
2
print(row.A)
3
print(row.Index)
4
since row is a named tuples, if you meant to access values on each row this should be MUCH faster
speed run :
JavaScript
1
13
13
1
df = pd.DataFrame([x for x in range(1000*1000)], columns=['A'])
2
st=time.time()
3
for index, row in df.iterrows():
4
row.A
5
print(time.time()-st)
6
45.05799984931946
7
8
st=time.time()
9
for row in df.itertuples():
10
row.A
11
print(time.time() - st)
12
0.48400020599365234
13