I practice Pandas and perform the following task:
Create a list whose elements are # columns of each .csv file
.csv are stored in a dictionary directory
with a key by year
I use dictionary understanding dataframes
(again on a year-by-year scale) to store CSV files as Pandas dataframes
directory = {2009: 'path_to_file/data_2009.csv', ... , 2018: 'path_to_file/data_2018.csv'}
dataframes = {year: pandas.read_csv(file) for year, file in directory.items()}
columns = [df.shape[1] for year, df in dataframes.items()]
columns = [dataframes[year].shape[1] for year in dataframes]
Which way is more Pythonic? Or is there a better way to approach this?
source
share