Load Multiple Csv Files Into Dataframe: Columns Names Issue
I have multiple csv files with the same format (14 rows 4 columns). I tried to load all of them into a single dataFrame, and use file's name to rename the values of the first colum
Solution 1:
I think you need create list of DataFrame
s first, then concat
with parameter keys
for new values by range
in MultiIndex
, then modify column id
and last remove MultiIndex
by reset_index
:
Also was added parameter names to read_csv
for custom columns names.
Y = []
forxin S_Sub_list:
n = ['id', 'data', 'isEB', 'isEM']
temp = pd.read_csv('sub' + str(x) +'_ICA_thre.csv', names = n)
Y.append(temp)
#list comprehension alternative
#n = ['id', 'data', 'isEB', 'isEM']
#Y = [pd.read_csv('sub' + str(x) +'_ICA_thre.csv', names = n) forxin S_Sub_list]
df = pd.concat(Y, keys=range(1,len(S_Sub_list) + 1))
df['id'] = 'sub' + df.index.get_level_values(0).astype(str) +'_ICA_'+ df['id'].astype(str)
df = df.reset_index(drop=True)
Post a Comment for "Load Multiple Csv Files Into Dataframe: Columns Names Issue"