Looping Over Data Sets

Останнє оновлення 2025-10-12 | Редагувати цю сторінку

Приблизний час: 15 хвилин

Огляд

Питання

  • How can I process many data sets with a single command?

Цілі

  • Навчіться читати та писати вирази модулю glob, які відповідають наборам файлів.
  • Використовуйте модуль glob для створення списків файлів
  • Напишіть цикли for для виконання операцій над файлами, зазначеними в списку.

Use a for loop to process files given a list of their names.


  • Ім’я файлу - це рядок символів.
  • І списки можуть містити рядки символів.

PYTHON

import pandas as pd
for filename in ['data/gapminder_gdp_africa.csv', 'data/gapminder_gdp_asia.csv']:
    data = pd.read_csv(filename, index_col='country')
    print(filename, data.min())

ВИХІД

data/gapminder_gdp_africa.csv gdpPercap_1952    298.846212
gdpPercap_1957    335.997115
gdpPercap_1962    355.203227
gdpPercap_1967    412.977514
⋮ ⋮ ⋮
gdpPercap_1997    312.188423
gdpPercap_2002    241.165877
gdpPercap_2007    277.551859
dtype: float64
data/gapminder_gdp_asia.csv gdpPercap_1952    331
gdpPercap_1957    350
gdpPercap_1962    388
gdpPercap_1967    349
⋮ ⋮ ⋮
gdpPercap_1997    415
gdpPercap_2002    611
gdpPercap_2007    944
dtype: float64

Використовуйте glob.glob, щоб знайти набори файлів, імена яких відповідають шаблону.


  • В Unix термін “globbing” означає “відповідність набору файлів шаблону”.
  • The most common patterns are:
    • * означає “відповідати нулю або більшій кількості символів”
    • ? означає “відповідати в точності одному символу”
  • Python містить бібліотеку glob для забезпечення функції зіставлення шаблонів
  • Бібліотека glob містить функцію, яка також називається glob для відповідності шаблонам файлів.
  • E.g., glob.glob('*.txt') matches all files in the current directory whose names end with .txt.
  • Результатом є (можливо, порожній) список рядків символів.

PYTHON

import glob
print('all csv files in data directory:', glob.glob('data/*.csv'))

ВИХІД

all csv files in data directory: ['data/gapminder_all.csv', 'data/gapminder_gdp_africa.csv', \
'data/gapminder_gdp_americas.csv', 'data/gapminder_gdp_asia.csv', 'data/gapminder_gdp_europe.csv', \
'data/gapminder_gdp_oceania.csv']

PYTHON

print('all PDB files:', glob.glob(' .pdb'))

ВИХІД

all PDB files: []

Використовуйте glob і for для обробки пакетів файлів.


  • Helps a lot if the files are named and stored systematically and consistently so that simple patterns will find the right data.

PYTHON

for filename in glob.glob('data/gapminder_*.csv'):
    data = pd.read_csv(filename)
    print(filename, data['gdpPercap_1952'].min())

ВИХІД

data/gapminder_all.csv 298.8462121
data/gapminder_gdp_africa.csv 298.8462121
data/gapminder_gdp_americas.csv 1397.717137
data/gapminder_gdp_asia.csv 331.0
data/gapminder_gdp_europe.csv 973.5331948
data/gapminder_gdp_oceania.csv 10039.59564
  • Це включає всі дані, а також дані по регіонах.
  • Використовуйте більш конкретний шаблон у вправах, щоб виключити весь набір даних.
  • But note that the minimum of the entire data set is also the minimum of one of the data sets, which is a nice check on correctness.
Вправа

Визначення збігів

Який із цих файлів не відповідає виразу glob.glob('data/*as*.csv')?

  1. data/gapminder_gdp_africa.csv
  2. data/gapminder_gdp_americas.csv
  3. data/gapminder_gdp_asia.csv

1 is not matched by the glob.

Вправа

Мінімальний розмір файлу

Modify this program so that it prints the number of records in the file that has the fewest records.

PYTHON

import glob
import pandas as pd
fewest = ____
for filename in glob.glob('data/*.csv'):
    dataframe = pd.____(filename)
    fewest = min(____, dataframe.shape[0])
print('smallest file has', fewest, 'records')

Note that the DataFrame.shape() method returns a tuple with the number of rows and columns of the data frame.

PYTHON

import glob
import pandas as pd
fewest = float('Inf')
for filename in glob.glob('data/*.csv'):
    dataframe = pd.read_csv(filename)
    fewest = min(fewest, dataframe.shape[0])
print('smallest file has', fewest, 'records')

You might have chosen to initialize the fewest variable with a number greater than the numbers you’re dealing with, but that could lead to trouble if you reuse the code with bigger numbers. Python lets you use positive infinity, which will work no matter how big your numbers are. What other special strings does the float function recognize?

Вправа

Порівняння даних

Напишіть програму, яка читає регіональні набори даних і будує графік середнього ВВП на душу населення для кожного регіону в часі в одній діаграмі. Pandas will raise an error if it encounters non-numeric columns in a dataframe computation so you may need to either filter out those columns or tell pandas to ignore them.

This solution builds a useful legend by using the string split method to extract the region from the path ’data/gapminder\_gdp\_a\_specific\_region.csv’.

PYTHON

import glob
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1,1)
for filename in glob.glob('data/gapminder_gdp*.csv'):
    dataframe = pd.read_csv(filename)
    # extract <region> from the filename, expected to be in the format 'data/gapminder_gdp_<region>.csv'.
    # we will split the string using the split method and `_` as our separator,
    # retrieve the last string in the list that split returns (`<region>.csv`), 
    # and then remove the `.csv` extension from that string.
    # NOTE: the pathlib module covered in the next callout also offers
    # convenient abstractions for working with filesystem paths and could solve this as well:
    # from pathlib import Path
    # region = Path(filename).stem.split('_')[-1]
    region = filename.split('_')[-1][:-4]
    # extract the years from the columns of the dataframe 
    headings = dataframe.columns[1:]
    years = headings.str.split('_').str.get(1)
    # pandas raises errors when it encounters non-numeric columns in a dataframe computation
    # but we can tell pandas to ignore them with the `numeric_only` parameter
    dataframe.mean(numeric_only=True).plot(ax=ax, label=region)
    # NOTE: another way of doing this selects just the columns with gdp in their name using the filter method
    # dataframe.filter(like="gdp").mean().plot(ax=ax, label=region)
# set the title and labels
ax.set_title('GDP Per Capita for Regions Over Time')
ax.set_xticks(range(len(years)))
ax.set_xticklabels(years)
ax.set_xlabel('Year')
plt.tight_layout()
plt.legend()
plt.show()
Виноска

Dealing with File Paths

The pathlib module provides useful abstractions for file and path manipulation like returning the name of a file without the file extension. This is very useful when looping over files and directories. In the example below, we create a Path object and inspect its attributes.

PYTHON

from pathlib import Path

p = Path("data/gapminder_gdp_africa.csv")
print(p.parent)
print(p.stem)
print(p.suffix)

ВИХІД

data
gapminder_gdp_africa
.csv

Hint: Check all available attributes and methods on the Path object with the dir() function.

Ключові моменти
  • Use a for loop to process files given a list of their names.
  • Use glob.glob to find sets of files whose names match a pattern.
  • Використовуйте glob і for для обробки пакетів файлів.