How do you call a file name defined as list by each name?
*First, you default the method to import the csv file.
def f_read_csv(tgrt_csv):
trgt_csv_temp = '%s.csv' % (tgrt_csv)
tgrt_tbl = pd.read_csv("".join([get_csv_path,trgt_csv_temp]))
return tgrt_tbl
*Secondly, using the for csv I tried to bring in the name of each file in list.
for name in read_csv_list:
f_read_csv('%s' %name)
How can I get each csv file in the name of the list?
I'm having trouble understanding your question. I think you're asking how to append '.csv' to the elements of the list. Is that correct?
If so then you can use the map()
function to achieve it:
z = list(map(lambda ip:(ip+'.csv'),tgrt_csv)
Then you can load the contents using pd.read_csv()
method.
I've changed your function, to get the names out, because I am not sure it works, some parts of the puzzle are missing:-). But I maintained some characteristics like asking for the files extension. In order to use os.listdir we import os module.
import os
I've defined my path as a folder in my python working directory and stored 3 csv files in there.
path = 'somedir/'
Here is your new function:
def f_read_csv(tgrt_csv):
tgrt_tbl = [] #to store file names
for file in os.listdir(tgrt_csv): #access to the directory
if file.endswith('.csv'): #checking files with .csv extension
name_file = os.path.join(tgrt_csv, file)
tgrt_tbl.append(name_file)
return tgrt_tbl
Then you call the function f_read_csv and pass the path:
names = f_read_csv("somedir/")
The output, if you print it:
['somedir/file 1.csv', 'somedir/file 2.csv', 'somedir/file 3.csv']
If you want them as strings you can get them out of the list:
for name in names:
print(name)
somedir/file 1.csv
somedir/file 2.csv
somedir/file 3.csv
I often use list comprehension
to generate dataframes.
dfs = [f_read_csv(name) for name in read_csv_list]
and use pd.concat
to concat them
df = pd.concat(dfs)
and more, use glob
to generate files list
files = glob.glob("/path/to/target/files/*.csv")
do you meaning read a list of filename by pandas, and transfrom then to dataframe, if all csv file has same format, you can use dask.dataframe
import dask.dataframe as dd
ddf = dd.read_csv(f"{get_csv_path}*.csv")
df = ddf.compute()
dask is incompatible with pandas.
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.