在熊猫数据框中添加缺少组合的其他行

时间:2020-08-17 08:25:06

标签: pandas dataframe

我有一个D列,它具有其他列名[Col A,Col B,COL C]的值,我想添加其他缺少组合的行。我的数据框如下所示: Original Data

import pandas as pd
data={'colA':[0,0,0],'ColB':[0,0,0] ,'ColC':[0,0,0],'ColD':['ColA','ColA','ColB'],'Target':[1,1,1]}
    df=pd.DataFrame(data)
    print(df)

我需要生成的df为:

data={'colA':[0,0,0,0,0,0,0,0,0],'ColB':[0,0,0,0,0,0,0,0,0] ,'ColC':[0,0,0,0,0,0,0,0,0],'ColD':['ColA','ColB','ColC','ColA','ColB','ColC','ColB','ColA','ColC'],'Target':[1,0,0,1,0,0,1,0,0]} 
df=pd.DataFrame(data) 
print(df) 

Resulting Data needed

1 个答案:

答案 0 :(得分:0)

鉴于ColA,B,C的内容无关紧要,您只想在ColD和Target中重复值,它就变成了dict理解权。与pandas

无关
data={'colA':[0,0,0],'ColB':[0,0,0] ,'ColC':[0,0,0],'ColD':['ColA','ColA','ColB'],'Target':[1,1,1]}
df=pd.DataFrame(data)

pd.DataFrame({k:v*3 
              if k not in ["Target","ColD"] 
              else [1,0,0]*3 
              if k=="Target" else ["ColA","ColB", "ColC"]*3 
              for k,v in data.items()})