python/pandas转移数据框架的格式



我有一个python/pandas dataframe有两个列:(表列为列表)

    VNAME   SHEET
0   atnpi   [HSP, HHA, HO_, INP]
1   atupi   [HSP, HHA, INP]
2   carnm   [HB_]
3   clmid   [HSP, HHA, HB_, HO_, INP]
4   clseq   [HSP, HHA, HO_, INP]

我想将其传输到

之类的数据框
    VNAME   HSP    HHA   HB_   HO_  INP 
0   atnpi   TRUE   TRUE        TRUE TRUE
1   atupi   TRUE   TRUE             TRUE
2   carnm                TRUE
3   clmid   TRUE   TRUE  TRUE  TRUE TRUE
4   clseq   TRUE   TRUE        TRUE TRUE

您可以做到这一点如下:

df1 = df1.set_index('VNAME')
(df1['SHEET'].apply(pd.Series).stack()
            .reset_index(1, drop=True)
            .to_frame().assign(key=True)
            .set_index(0,append=True)['key']
            .unstack()
            .fillna('')
            .rename_axis(None,1)
            .reset_index())

输出:

   VNAME   HB_   HHA   HO_   HSP   INP
0  atnpi        True  True  True  True
1  atupi        True        True  True
2  carnm  True                        
3  clmid  True  True  True  True  True
4  clseq        True  True  True  True

或获得字符串'true'

(df1['SHEET'].apply(pd.Series).stack()
                  .reset_index(1, drop=True)
                  .to_frame().assign(key='TRUE')
                  .set_index(0,append=True)['key']
                  .unstack().fillna('')
                  .rename_axis(None,1)
                  .reset_index())

输出:

   VNAME   HB_   HHA   HO_   HSP   INP
0  atnpi        TRUE  TRUE  TRUE  TRUE
1  atupi        TRUE        TRUE  TRUE
2  carnm  TRUE                        
3  clmid  TRUE  TRUE  TRUE  TRUE  TRUE
4  clseq        TRUE  TRUE  TRUE  TRUE

选项2

df1 = df1.set_index('VNAME')
df2 = df1['SHEET'].apply(pd.Series).stack().reset_index(name='SHEET').drop('level_1', axis=1)
pd.crosstab(df2.VNAME, df2.SHEET).astype(bool).replace(False,'').reset_index().rename_axis(None,1)

输出:

   VNAME   HB_   HHA   HO_   HSP   INP
0  atnpi        True  True  True  True
1  atupi        True        True  True
2  carnm  True                        
3  clmid  True  True  True  True  True
4  clseq        True  True  True  True

最新更新