############################## #统计特征SB下XX数据有几个,并保存 #适用于 #featureA featureB featureC #SDF 345 TA #SDF 976 TB #KKj 3 TA #KKj 43 TB #想转为 #featureA TA TB #SDF 345 976 #KKj 43 3 ############################## import pandas as pd df=pd.read_csv('指定数据的文件名',encoding='utf8') featureC=list(set(list(df['featureC']))) print(featureC) featureA_list = ['数据划分列表'] list_basic_featureC=featureC.copy() head_str=[] dic_basic_id={} dic_use_id={} for i in list_basic_featureC: strs_id ="可以自定义名称" +str(i) head_str.append(strs_id) dic_basic_id[i]=strs_id dic_use_id[i]=0 head_str.append('featureA') Lists_tot = [head_str] print(Lists_tot) return_lists = featureC.copy() for k in range(0,len(featureA_list)): Ts = return_lists.copy() Tdic = dic_use_id.copy() dfs=df[(df['brand']==featureA_list[k])] if not dfs.empty : #逐行遍历dataFrame for r,i in dfs.iterrows(): featureB = i['featureB'] featureC=i['featureC'] Tdic[featureC]=featureB print(Tdic) for m in range(0,len(Ts)): Ts[m] = Tdic[return_lists[m]] Ts.append(featureA_list[k]) Lists_tot.append(Ts) print(Lists_tot) import pandas as pd dft = pd.DataFrame(Lists_tot[1:], columns=Lists_tot[0]) print(dft) print("1") dft.to_csv('输出文件结果', encoding='utf8', index=False) print("~end~")
标签:list,Lists,三元组,数据表,featureC,featureA,print,csv,id From: https://www.cnblogs.com/AKsnoopy/p/16639392.html