lserlohn
lserlohn

Reputation: 6206

Pandas: access data from dataframe by row and column number

I have a simple program made me confused. I read a 3 * 10 data from a csv file, and I want to access a particular data by its row and column number. But it failed, I doun't know why.

matrix.txt:

1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21,22,23,24,25,26,27,28,29,30

Program:

datainput = pd.read_csv('matrix.txt',sep=',', header = None)

inputinfo = datainput.shape ==> 3, 10

print datainput[3][3] => failed,but it should return 23, 

I can not access any data if the column number is equal or greater than 3

Upvotes: 3

Views: 9401

Answers (1)

EdChum
EdChum

Reputation: 393933

Indexing starts from 0:

In [8]:

df
Out[8]:
    0   1   2   3   4   5   6   7   8   9
0   1   2   3   4   5   6   7   8   9  10
1  11  12  13  14  15  16  17  18  19  20
2  21  22  23  24  25  26  27  28  29  30

In [11]:

df[2][2]
Out[11]:
23

As you've not supplied a header one is auto generated and also for the index, the default behaviour is to generate indices starting from 0 as you can see above.

Also your last statement is not true:

In [13]:

df[3][2], df[5][2]
Out[13]:
(24, 26)

Here the first subscript value is the column label followed by the row label.

The following does raise a KeyError:

df[3][3]

Upvotes: 3

Related Questions