I have a set of data, and I accidentally put the column name in the last row of the table when reading, so the column name of the first row of the table is the default column name, Var1, etc., and the format is as follows. The requirement now is to replace the default column names with the column names of the last row.
The first step, read the form
//.csv文件读取办法
data = pd.read_csv('文件名.csv')
The second step is to get the correct column name of the table, that is, the content of the last row
columns = data.loc[len(data)-1]
columns
The third step is to convert the real label value obtained in the previous step into a list, replace the default label value, and delete the last line
data.columns =list(data.columns)
data = data.drop([len(data)-1])
data
If multiple tables in a folder need to be processed in batches, you can use a loop, the program is as follows
filePath = r'文件夹路径名'forfilein os.listdir(filePath):
excelFile = os.path.join(filePath,file)
data = pd.read_csv(excelFile)
columns = data.loc[len(data)-1]
data.columns =list(columns)
data = data.drop([len(data)-1])
data.to_csv(excelFile, index=False)