Pythonic way to loop over dictionary

Vivek Jha

I am practicing Pandas and have the following task:

Create a list whose elements are the # of columns of each .csv file


.csv files are stored in the dictionary directory keyed by year

I use a dictionary comprehension dataframes (again keyed by year) to store the .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?

piRSquared

Your method will get it done... but I don't like reading in the entire file and creating a dataframe just to count the columns. You could do the same thing by just reading the first line of each file and counting the number of commas. Notice that I add 1 because there will always be one less comma than there are columns.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pythonic way to make dictionary within a for loop

From Dev

Pythonic way of executing a loop on a dictionary of lists

From Dev

What is the most pythonic way to create a 'For Loop' that filters through a dictionary?

From Dev

Pythonic way to loop through dictionary and perform conditional GET request

From Dev

A Pythonic way to "query" a dictionary

From Dev

Pythonic way of splitting loop over list in two parts with one iterator

From Dev

Pythonic way to create a dictionary by iterating

From Dev

pythonic way of setting values in a dictionary

From Dev

Pythonic way to create a nested dictionary

From Dev

create list of dictionary in a pythonic way

From Dev

pythonic way of reassigning values to dictionary

From Dev

Is there a Pythonic way to map a dictionary to another dictionary?

From Dev

Is there a more pythonic/more efficient way to loop through dictionary containing lists rather than using for loops?

From Java

Pythonic way to combine FOR loop and IF statement

From Dev

Pythonic way to default loop counters

From Dev

Pythonic way to break out of loop

From Dev

Pythonic way to loop over an array starting from i while maintaining the original array's indexes

From Dev

Loop over a dictionary efficiently

From

Is there a way to iterate over a dictionary?

From Dev

What is the pythonic way to to resume a loop within a loop

From Dev

Pythonic Way to Add New Keys into Dictionary of Lists

From Python

Pythonic way to obtain a dictionary from a set

From Dev

Pythonic way to pass dictionary as argument with specific keys

From Dev

Pythonic way to find and replace with a dictionary of lists

From Dev

Most pythonic way to append Dictionary with specific keys

From Dev

Is there a more pythonic/compact way to create this dictionary?

From Dev

Pythonic way to populate a dictionary with count of values

From Python

Pythonic way to replace values in a list according to a dictionary

From Dev

Pythonic way to populate a dictionary from list of records

Related Related

  1. 1

    Pythonic way to make dictionary within a for loop

  2. 2

    Pythonic way of executing a loop on a dictionary of lists

  3. 3

    What is the most pythonic way to create a 'For Loop' that filters through a dictionary?

  4. 4

    Pythonic way to loop through dictionary and perform conditional GET request

  5. 5

    A Pythonic way to "query" a dictionary

  6. 6

    Pythonic way of splitting loop over list in two parts with one iterator

  7. 7

    Pythonic way to create a dictionary by iterating

  8. 8

    pythonic way of setting values in a dictionary

  9. 9

    Pythonic way to create a nested dictionary

  10. 10

    create list of dictionary in a pythonic way

  11. 11

    pythonic way of reassigning values to dictionary

  12. 12

    Is there a Pythonic way to map a dictionary to another dictionary?

  13. 13

    Is there a more pythonic/more efficient way to loop through dictionary containing lists rather than using for loops?

  14. 14

    Pythonic way to combine FOR loop and IF statement

  15. 15

    Pythonic way to default loop counters

  16. 16

    Pythonic way to break out of loop

  17. 17

    Pythonic way to loop over an array starting from i while maintaining the original array's indexes

  18. 18

    Loop over a dictionary efficiently

  19. 19

    Is there a way to iterate over a dictionary?

  20. 20

    What is the pythonic way to to resume a loop within a loop

  21. 21

    Pythonic Way to Add New Keys into Dictionary of Lists

  22. 22

    Pythonic way to obtain a dictionary from a set

  23. 23

    Pythonic way to pass dictionary as argument with specific keys

  24. 24

    Pythonic way to find and replace with a dictionary of lists

  25. 25

    Most pythonic way to append Dictionary with specific keys

  26. 26

    Is there a more pythonic/compact way to create this dictionary?

  27. 27

    Pythonic way to populate a dictionary with count of values

  28. 28

    Pythonic way to replace values in a list according to a dictionary

  29. 29

    Pythonic way to populate a dictionary from list of records

HotTag

Archive