Pandas, How to read a CSV file

Syntax:


df = pd.read_csv('input.csv', 
                    index_col='COLUMN_NAME', 
                    usecols=[0, 2, 3])

Pandas reads the content of of CSV file using the read_csv method and the file name. The index column and the column used are optional parameters.

Example:

This code example imports the Pandas library, loads the content of the file called input.csv. The index column is column A and the system only imports the first 3rd and 4th columns. The dataframe loaded is then written in the output.


import pandas as pd 

df = pd.read_csv('input.csv', 
                    index_col='A', 
                    usecols=[0, 2, 3])

print( df )

The output will be:

     C   D
A         
1    3   4
5    7   8
9   11  12
13  15  16
17  19  20

References:

Pandas CSV

Recent Comments