#%%
import pandas as pd
df=pd.read_excel('工作表.xlsx')
col_list=list(df.columns)
del_col_list =['c','d'] #需要删除的字段名
# 构造备注列
df['beizhu']=''
for column in del_col_list:
df['beizhu']=df['beizhu']+f'''"{column}":'''+'"'+df[column].astype(str)+'",'
df['beizhu']='{'+df['beizhu'].astype(str).apply(lambda x:x.strip(','))+'}'
#把要删除的字段在col_list中删去,并加上'beizhu'字段
for del_col in del_col_list:
col_list.remove(del_col)
col_list.append('beizhu')
print(col_list)
#根据col_list 保留列
df=df[col_list]
#OK了
df.to_excel('666.xlsx',index=False)
标签:beizhu,df,list,pandas,json,del,dic,col From: https://www.cnblogs.com/o0o0o9/p/16742703.html