2017-03-17 23 views
1

我有一個多指標,從使用.extractall()結果:採取的大熊貓多指標值,並讓他們到一個列表,併爲多列

     0  1 2  3 4 5 6 7 8 \ 
     match 
0  0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
1  0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
2  0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
3  0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
5  0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
6  0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
7  0   Canvas Canvas NaN  NaN NaN NaN NaN NaN NaN 
8  0   Canvas Canvas NaN  NaN NaN NaN NaN NaN NaN 
9  0   Canvas Canvas NaN  NaN NaN NaN NaN NaN NaN 
10 0   Canvas Canvas NaN  NaN NaN NaN NaN NaN NaN 
11 0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
12 0   Canvas Canvas NaN  NaN NaN NaN NaN NaN NaN 
     1  Calf Leather  NaN NaN  NaN NaN NaN NaN NaN NaN 
15 0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
16 0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
17 0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
18 0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 
20 0   Leather  NaN NaN Leather NaN NaN NaN NaN NaN 

如果你看一下指數12,這表明該條目有兩個匹配,「帆布」和「小牛皮」。我怎樣才能從這個多重索引轉換成顯示所有匹配屬性的列?我想知道如何以兩種方式做到這一點。這是第一個結果,我想:

    material  

0    Leather  
1    Leather  
2    Leather  
3    Leather 
4     Nan  
5    Leather  
6    Leather  
7     Canvas 
8     Canvas 
9     Canvas 
10    Canvas 
11    Leather 
12 Canvas, Calf Leather 
13     NaN 
14     Nan 
15    Leather 
16    Leather 
17    Leather 
18    Leather 
19     Nan 
20    Leather 

這需要所有的多指標的每一級的結果,並把它們放入一個列表。您會注意到我只關注原始多重索引中的第0列,這是所有來自.extractall的結果得到彙總的地方。這是第二個結果,我想創造:

    material  material1 

0    Leather  NaN 
1    Leather  NaN 
2    Leather  NaN 
3    Leather  NaN 
4     NaN  NaN 
5    Leather  NaN 
6    Leather  NaN 
7     Canvas  NaN 
8     Canvas  NaN 
9     Canvas  NaN 
10    Canvas  NaN 
11    Leather  NaN 
12    Canvas Calf Leather 
13     NaN  NaN 
14     NaN  NaN 
15    Leather  NaN 
16    Leather  NaN 
17    Leather  NaN 
18    Leather  NaN 
19     NaN  NaN 
20    Leather  NaN 

對於第二個結果,會有許多附加列作爲.extractall多指標匹配的數量最多。

我很高興澄清任何不清楚的東西。謝謝!

回答

0

我認爲你可以使用dropna與所有NaN s的總join刪除列,然後groupby

df1 = df.dropna(axis=1, how='all').groupby(level=0).agg(lambda x: ', '.join(x.dropna())) 
#replace to None empty spaces 
df1 = df1.replace({'': None}) 
print (df1) 
         0  1  3 

0    Leather None Leather 
1    Leather None Leather 
2    Leather None Leather 
3    Leather None Leather 
5    Leather None Leather 
6    Leather None Leather 
7     Canvas Canvas  None 
8     Canvas Canvas  None 
9     Canvas Canvas  None 
10    Canvas Canvas  None 
11    Leather None Leather 
12 Canvas, Calf Leather Canvas  None 
15    Leather None Leather 
16    Leather None Leather 
17    Leather None Leather 
18    Leather None Leather 
20    Leather None Leather 

對於同一列長度使用unstack然後list comprehension刪除MultiIndex在列:

df2 = df.dropna(axis=1, how='all').unstack() 
df2.columns = ['mat{}_{}'.format(x[0], x[1]) for x in df2.columns] 
print (df2) 
    mat0_0  mat0_1 mat1_0 mat1_1 mat3_0 mat3_1 

0 Leather   None  NaN None Leather None 
1 Leather   None  NaN None Leather None 
2 Leather   None  NaN None Leather None 
3 Leather   None  NaN None Leather None 
5 Leather   None  NaN None Leather None 
6 Leather   None  NaN None Leather None 
7 Canvas   None Canvas None  NaN None 
8 Canvas   None Canvas None  NaN None 
9 Canvas   None Canvas None  NaN None 
10 Canvas   None Canvas None  NaN None 
11 Leather   None  NaN None Leather None 
12 Canvas Calf Leather Canvas NaN  NaN NaN 
15 Leather   None  NaN None Leather None 
16 Leather   None  NaN None Leather None 
17 Leather   None  NaN None Leather None 
18 Leather   None  NaN None Leather None 
20 Leather   None  NaN None Leather None 
+0

看起來真不錯!唯一缺少的是填充沒有結果的行。因此,在上面的兩個解決方案中,第4,13,19行以及所有其他類似的應該在第0列和mat0_1中分別具有「無」作爲它們的值。 – thaneofcawdor

+0

我想你需要添加reindex的範圍,如'df1 = df.dropna(axis = 1,how ='all')。groupby(level = 0).agg(lambda x:','.join(x.dropna )))。重新索引(範圍(LEN(df.index)))' – jezrael