给定 pandas 当前正在使用熔化函数进行重塑的数据框,是否可以有一种可能的替代方法(代码)来重塑?

can there be a possible alternative (code) to reshape given pandas dataframe that is currently using melt function for reshaping?

我有一个 pandas 数据框,如下所示:

Category            Sector 1    Blanks  Sector 2    Sector 3    Sector 4    Sector 5    Sector 6    Other   Sector 7
Blank                   0           1       0           0           0           0           0           0       0
3D                      1           0       0           0           0           1           1           0       0
3DPrinting              0           0       1           0           0           1           1           0       0
3DTechnology            0           0       0           1           0           1           1           0       0
B2B                     0           0       0           0           1           0           0           0       1
B2B Express Delivery    0           0       0           0           0           0           0           0       1
K-12 Education          0           0       0           0           0           0           0           1       0
M2M                     0           0       0           0           0           0           0           1       0
P2P Money Transfer      0           0       0           0           1           0           0           0       1

我正在尝试使用以下代码重塑此数据集(只会选择第一个扇区):

cols = list(df.columns.difference(['Category']))
cols
df = pd.melt(df, id_vars=['Category'], value_vars=cols) 
df = df[~(df.value == 0)]
df = df.drop('value', axis = 1)
df = df.rename(columns = {"variable" : "Sectors"})
df.head()

哪个完成了工作,我能够根据需要重塑数据框,如下所示:

Category                Sectors
3D                      Sector 1
3DPrinting              Sector 2
3DTechnology            Sector 3
B2B                     Sector 4
B3B Express Delivery    Sector 7
....
....

不知何故我不满意,正在寻找上面许多行代码的替代方法。

有可能吗?

试试 melt:

(df.melt('Category', var_name='Sectors')
   .query('value>0 & Sectors != "Blanks"')
)

输出:

                Category   Sectors  value
1                     3D  Sector 1      1
20            3DPrinting  Sector 2      1
30          3DTechnology  Sector 3      1
40                   B2B  Sector 4      1
44    P2P Money Transfer  Sector 4      1
46                    3D  Sector 5      1
47            3DPrinting  Sector 5      1
48          3DTechnology  Sector 5      1
55                    3D  Sector 6      1
56            3DPrinting  Sector 6      1
57          3DTechnology  Sector 6      1
69        K-12 Education     Other      1
70                   M2M     Other      1
76                   B2B  Sector 7      1
77  B2B Express Delivery  Sector 7      1
80    P2P Money Transfer  Sector 7      1