There's more...

It is possible to read all files from a particular directory into DataFrames without knowing their names. Python provides a few ways to iterate through directories, with the glob module being a popular choice. The gas prices directory contains five different CSV files, each having weekly prices of a particular grade of gas beginning from 2007. Each file has just two columns--the date for the week and the price. This is a perfect situation to iterate through all the files, read them into DataFrames, and combine them all together with the concat function. The glob module has the glob function, which takes a single parameter--the location of the directory you would like to iterate through as a string. To get all the files in the directory, use the string *. In this example, *.csv returns only files that end in .csv. The result from the glob function is a list of string filenames, which can be directly passed to the read_csv function:

>>> import glob

>>> df_list = []
>>> for filename in glob.glob('data/gas prices/*.csv'):
df_list.append(pd.read_csv(filename, index_col='Week',
parse_dates=['Week']))

>>> gas = pd.concat(df_list, axis='columns')
>>> gas.head()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.45.80