PANDAS DATAFRAMES AND CSV FILES
The code samples in several earlier sections contain hard-coded data inside the Python scripts. However, it’s also very common to read data from a CSV file. You can use the Python csv.reader() function, the NumPy loadtxt() function, or the Pandas function read_csv() function (shown in this section) to read the contents of CSV files.
Listing 3.17 displays the contents of the CSV file weather_data.csv and Listing 3.18 displays the contents of weather_data.py that illustrates how to read the CSV weather_data.csv.
LISTING 3.17: weather_data.csv
day,temperature,windspeed,event 7/1/2018,42,16,Rain 7/2/2018,45,3,Sunny 7/3/2018,78,12,Snow 7/4/2018,74,9,Snow 7/5/2018,42,24,Rain 7/6/2018,51,32,Sunny
LISTING 3.18: weather_data.py
import pandas as pd
df = pd.read_csv("weather_data.csv")
print(df)
print(df.shape) # rows, columns
print(df.head()) # df.head(3)
print(df.tail())
print(df[1:3])
print(df.columns)
print(type(df['day']))
print...