简体   繁体   中英

python 2.7 rename values within list

When import several csvs and I save it within an array, the name for all files that were imported is q=[Dataframe, Dataframe,Dataframe,Dataframe,Dataframe,Dataframe,Dataframe,Dataframe] , I would like to change the name using like base the name of the file.

files_array_Q = []
files_array_F = []
files_array_MRG =[]

for files in files_import_Q :
    qs_matrix = pd.read_csv(files, delimiter=" ", header=None)
    files_array_Q.append(qs_matrix)

for files in files_import_F :
    in_fam = pd.read_csv(files, delimiter=" ", header=None)
    files_array_F.append(in_fam)

for example read files with names file1.Q file2.Q file3.Q files4.Q

within the array files_array_Q = [files1, file2, files3, file4]

You can use the Pandas string split function . This solution assumes that the file names do not have extraneous periods.

q_list = pd.DataFrame(['file1.Q', 'file2.Q', 'file3.Q'], columns=['files'])

# split into file, type
q_split = pd.DataFrame(q.files.str.split('.',1).tolist())

# now get only the first column:
q_name_only = q_split[q_split.columns[0]]

You can combine these two steps into one line using Pandas iloc to choose the column by its integer location:

q_name_only = pd.DataFrame(q_list.files.str.split('.',1).tolist()).iloc[:,0]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM