2 回答
TA贡献1806条经验 获得超8个赞
实际上它确实有效,但我想知道是否有更快的解决方案。这是代码:
df1 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df2 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df3 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df4 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df5 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df6 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df7 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df8 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df9 = pd.DataFrame(columns=['CNTR_CODE', 'Risk_damage', 'pot_tot_loss','annual_tot_loss'])
df1[["CNTR_CODE", 'Risk_damage', 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'Risk_damage0','pot_tot_loss00', 'annual_tot_loss00']]
df2[["CNTR_CODE", 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'pot_tot_loss01','annual_tot_loss01']]
df3[["CNTR_CODE", 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'pot_tot_loss02','annual_tot_loss02']]
df4[["CNTR_CODE", 'Risk_damage', 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'Risk_damage1','pot_tot_loss10', 'annual_tot_loss10']]
df5[["CNTR_CODE", 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'pot_tot_loss11','annual_tot_loss11']]
df6[["CNTR_CODE", 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'pot_tot_loss12','annual_tot_loss12']]
df7[["CNTR_CODE", 'Risk_damage', 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'Risk_damage2','pot_tot_loss20', 'annual_tot_loss20']]
df8[["CNTR_CODE", 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'pot_tot_loss21','annual_tot_loss21']]
df9[["CNTR_CODE", 'pot_tot_loss','annual_tot_loss']]=copy_df[["CNTR_CODE", 'pot_tot_loss22','annual_tot_loss22']]
new_df=pd.concat([df1, df2,df3,df4,df5,df6,df7,df8,df9]).reset_index()
TA贡献1875条经验 获得超5个赞
首先像这样获取临时数据框
df1[["a", "b"]]=df[["a1", "b1"]]
df2[["a", "b"]]=df[["a2", "b2"]]
这将给出两个数据帧,然后连接两者
new_df=pd.concat([df1, df2]).reset_index()
添加回答
举报