Reading In FilesΒΆ
Loading external data is the first step in nearly every analysis. Pandas provides dedicated read_* functions for CSV, TSV, JSON, Excel, and many other formats, each with parameters for handling delimiters, headers, data types, encodings, and sheet selection.
This notebook demonstrates reading CSV with pd.read_csv(), using pd.read_table() with a custom separator, loading JSON with pd.read_json(), reading Excel workbooks with pd.read_excel() and sheet selection, configuring display options with pd.set_option() for max rows and columns, and performing initial data inspection with .info(), .shape, .head(), .tail(), column selection, .loc[], and .iloc[].
import pandas as pd
df = pd.read_csv(r"C:\Users\alexf\OneDrive\Documents\Pandas Tutorial\countries of the world.csv")
df
#df = pd.read_csv(r"C:\Users\alexf\OneDrive\Documents\Pandas Tutorial\countries of the world.txt", sep = '\t')
#df
df = pd.read_table(r"C:\Users\alexf\OneDrive\Documents\Pandas Tutorial\countries of the world.csv", sep = ',')
df
df = pd.read_json(r"C:\Users\alexf\OneDrive\Documents\Pandas Tutorial\json_sample.json")
df
df2 = pd.read_excel(r"C:\Users\alexf\OneDrive\Documents\Pandas Tutorial\world_population_excel_workbook.xlsx", sheet_name = 'Sheet1')
df2
pd.set_option('display.max.rows', 235)
pd.set_option('display.max.columns', 40)
df2.info()
df2.shape
df2.head(10)
df2.tail(10)
df2['Rank']
df2.loc['Uzbekistan']
df2.iloc[224]