Pythonic way to iterate over a dictionary

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 directorywith 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()}

# My Approach 1 
columns = [df.shape[1] for year, df in dataframes.items()]

# My Approach 2
columns = [dataframes[year].shape[1] for year in dataframes]

Which way is more Pythonic? Or is there a better way to approach this?

+4
source share
4 answers

... dataframe . , . , 1, , .

columns = [open(f).readline().count(',') + 1 for _, f in directory.items()]
+4

2:

columns = [dataframes[year].shape[1] for year in dataframes]

Pythonic dataframes , , .., ,

+4

:

columns = [len(dataframe.columns) for dataframe in dataframes.values()]

@piRSquared, - , csv, nrows read_csv.

+3
import os
#use this to find files under certain dir, you can filter it if there are other files
target_files = os.listdir('path_to_file/')       
columns = list()
for filename in train_files:
    #in your scenario @piRSquared answer would be more efficient.
    columns.append(#column_numbers) 

, :

year = filename.replace(r'[^0-9]', '')
+2

Source: https://habr.com/ru/post/1689290/


All Articles