pandas uses the last row of the table instead of the first row as the column name (that is, modify the column name)

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.

insert image description here

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

insert image description here

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

insert image description here

If multiple tables in a folder need to be processed in batches, you can use a loop, the program is as follows

filePath = r'文件夹路径名'

for file in 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)

Guess you like

Origin blog.csdn.net/weixin_40061485/article/details/123522671