I have the CSV-file and I need to combine its first four rows to headers (and first header needs to be empty). I tried to do that with Pandas and code works but I know that looping through dataframe is not recommended. Is there better way to do it?
import pandas as pd
df = pd.read_csv(file_path, header=None)
df_headers = df[0:4]
df = df.drop(index=[0,1,2,3,])
header_list = []
for column in df_headers:
header_list.append(df_headers[column].str.cat(sep=' '))
header_list[0] = ""
df.columns = header_list
df.to_csv('output.csv',index=False)
>>> columns_mapping = dict(zip(range(df.shape[1]), df.iloc[:4].astype(str).apply(' '.join).to_list()))
>>> df = df.iloc[4:].rename(columns=columns_mapping)
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.