Spider Man Spider Man - 1 year ago 148
Python Question

How to read all rows of a csv file using pandas in python?

I am using the

module for reading the data from a

I can write out the following code to extract the data belonging to an individual column as follows:

import pandas as pd

df = pd.read_csv('somefile.tsv', sep='\t', header=0)
some_column = df.column_name
print some_column # Gives the values of all entries in the column

However, the file that I am trying to read now has more than 5000 columns and writing out the statement

some_column = df.column_name

is now not feasible. How can I get all the column values so that I can access them using indexing?

e.g to extract the value present at the 100th row and the 50th column, I should be able to write something like this:


Answer Source

Use DataFrame.iloc or DataFrame.iat, but python counts from 0, so need 99 and 49 for select 100. row and 50. column:

df = df.iloc[99,49]

Sample - select 3. row and 4. column:

df = pd.DataFrame({'A':[1,2,3],

print (df)
   A  B  C   D  E  F
0  1  4  7   1  5  7
1  2  5  8   3  3  4
2  3  6  9  10  6  3

print (df.iloc[2,3])

print (df.iat[2,3])

Combination for selecting by column name and position of row is possible by Series.iloc or Series.iat:

print (df['D'].iloc[2])

print (df['D'].iat[2])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download