1

I am using python 2.7 and pandas 0.20.3.

Consider following simple dataframe code.

import pandas as pd

days = ["Mon", "Tue", "Thu", "Fri"]
years = [2000, 2001, 2002, 2003, 2004]

x = [ #  Mon     Tue     Thu     Fri
        [26.16,  27.16,  25.69,  22.81],    # 2000   Row 1
        [20.75,  21.32,  18.20,  16.08],    # 2001   Row 2
        [16.42,  18.32,  18.59,  18.02],    # 2002   Row 3
        [14.56,  14.32,  13.85,  13.20],    # 2003   Row 4
        [21.02,  20.32,  20.78,  19.90]     # 2004   Row 5
    ] # Col1     Col2    Col3    Col4

df = pd.DataFrame(x, columns = days, index = years)
print df

I need help with following:

  1. How to print 4th column from beginning, i.e. corresponding to Fri?
  2. How to print 2nd row from end, i.e. corresponding to 2003?
  3. How to iterate data of every cell in row major manner?
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Subhajit Kundu
  • 383
  • 1
  • 2
  • 13

1 Answers1

4

Use iloc for select by positions and iterrows, but maybe exist some faster vectorized alternatives for your problem, because it is really slow:

#python counts from 0, so for 4.th column
a = df.iloc[:, 3]
print (a)
2000    22.81
2001    16.08
2002    18.02
2003    13.20
2004    19.90
Name: Fri, dtype: float64

#from back for second column
b = df.iloc[-2]
print (b)
Mon    14.56
Tue    14.32
Thu    13.85
Fri    13.20
Name: 2003, dtype: float64

for i, row in df.iterrows():
    print (i)
    print (row)

If want select by columns / index labels:

a1 = df['Fri']
print (a1)
2000    22.81
2001    16.08
2002    18.02
2003    13.20
2004    19.90
Name: Fri, dtype: float64

b1 = df.loc[2003]
print (b1)
Mon    14.56
Tue    14.32
Thu    13.85
Fri    13.20
Name: 2003, dtype: float64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252