Looping through multiple CSV’s and creating a dataframe of all them combined

I’m trying to create a data frame using multiple CSV files. For this, I created a for loop but it only runs once. All data is already downloaded in CSV format.

csvs = [
    "2014-2015.csv",
    "2015-2016.csv",
    "2016-2017.csv",
    "2017-2018.csv",
    "2018-2019.csv",
    "2019-2020.csv",
    "2020-2021.csv",
    "2021-2022.csv"
]
 
for csv in csvs: 
    data = pd.read_csv(csv)
    data_final = pd.DataFrame()
    data_final = data_final.append(data)

Answer

You can do that with a comprehension like this:

import pandas as pd

csv_files = [
    '2014-2015.csv',
    '2015-2016.csv',
    '2016-2017.csv',
    '2017-2018.csv',
    '2018-2019.csv',
    '2019-2020.csv',
    '2020-2021.csv',
    '2021-2022.csv'
]

combined_df = pd.concat([pd.read_csv(_) for _ in csv_files])