<jupyter_start><jupyter_text>This script was shared by "inversion" on Kaggle "Right Whale Recognition" discussion forum. It is supposed to be run from the 'Kaggle_Whale' directory.https://www.kaggle.com/c/noaa-right-whale-recognition/discussion/16275Before running the script, make sure all image files in the dataset have been extracted into the './imgs/' sub-folder. Also make sure 'train.csv' is located in current  directory. More specifically, execute the following shell commands to process files downloaded directly from Kaggle web site.```shell$ unzip imgs.zip$ unzip w_7489.jpg.zip$ mv w_7489.jpg imgs/$ unzip train.csv.zip```After running the script, the training images would be sorted into sub-folder named by whale ID in the csv, e.g. './imgs/w_7001.jpg' -> './imgs/whale_34663/w_7001.jpg.<jupyter_code>import os
import pandas as pd

train = pd.read_csv('train.csv', index_col='Image')

whaleIDs = list(train['whaleID'].unique())

for w in whaleIDs:
    os.makedirs('./imgs/'+w)

for image in train.index:
    folder = train.loc[image, 'whaleID']
    old = './imgs/{}'.format(image)
    new = './imgs/{}/{}'.format(folder, image)
    try:
        os.rename(old, new)
    except:
        print('{} - {}'.format(image,folder))<jupyter_output><empty_output><jupyter_text>Compare number of sorted images (jpg files) with number of labels in 'train.csv'.<jupyter_code>!ls -1 ./imgs/*/*.jpg | wc -l
!grep jpg train.csv | wc -l<jupyter_output>4544
4544
{'fraction_non_alphanumeric': 0.13237311385459533, 'fraction_numerical': 0.02400548696844993, 'mean_word_length': 4.229390681003585, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '239943', 'license': 'MIT', 'n_tokens_mistral': 516, 'n_tokens_neox': 489, 'n_words': 151, 'path': 'Kaggle_Whale/sort_imgs.ipynb', 'repo_name': 'jkjung-avt/dataset-tools', 'size': '1458'}