2.4. Importing data into DataFrames
and exploring its attributes#
You can also make a dataframes
from any csv file (excel files, delimited text files, etc). The table below summarizes the important functions you need to remember when applying the pd.read_csv()
command.
Aspect |
Description |
Function |
---|---|---|
File Path |
Specify the path or filename of the CSV file |
pd.read_csv(‘file_path.csv’) |
Delimiter |
Define the delimiter used in the CSV file |
pd.read_csv(‘file.csv’, delimiter=’,’) |
Header |
Specify whether the CSV file has a header row |
pd.read_csv(‘file.csv’, header=0) |
Columns |
Select specific columns from the CSV file |
pd.read_csv(‘file.csv’, usecols=[‘col1’, ‘col2’]) |
Index Column |
Set a specific column as the index of the DataFrame |
pd.read_csv(‘file.csv’, index_col=’column’) |
Data Types |
Specify data types for columns in the DataFrame |
pd.read_csv(‘file.csv’, dtype={‘col1’: int, ‘col2’: float}) |
Note
The excel file needs to be in the same place in the folder that the python file is at for this to work!