Downloads/challenges
). azerbaijan_challenge_entrants.csv
. I want to create one new CSV ( all_entrants.csv
) that includes all data from all 100 CSVs, adding one new column: challenge
, which should include the name of the CSV that the row data came from.
I generally like Python for tasks like this. But I am struggling to make this work. Any help would be appreciated!
This is possible with os
from the standard library and 3rd party library pandas
:
import os
import pandas as pd
mypath = os.path.join('Downloads', 'challenges')
# get list of files
files = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
# build list of dataframes, adding "challenge" column
dfs = [pd.read_csv(os.path.join(mypath, f)).assign(challenge=f) for f in files]
# concatenate dataframes into one
df = pd.concat(dfs, ignore_index=True)
# write to csv
df.to_csv('all_entrants.csv')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.