I have several csv with a string in their name (e.g city name) and want to read them in dataframe with the names derived from that city name.
example of csv names: data_paris.csv , data_berlin.csv
How can I read them in a loop to get df_paris and df_berlin?
What I tried so far:
all_files = glob.glob(./*.csv")
for filename in all_files:
city_name=re.split("[_.]", filename)[1] #to extract city name from filename
dfname= {'df' + str(city_name)}
print(dfname)
dfname= pd.read_csv(filename)
I expect to have df_rome and df_paris, but I get just dfname. Why?
A related question: Name a dataframe based on csv file name?
Thank you!
df_parisanddf_berlin, you should create a dictionarydfswith keys'paris'and'berlin', so you can dodfs['paris']anddfs['berlin'].