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
多指標匹配的數量最多。
我很高興澄清任何不清楚的東西。謝謝!
看起來真不錯!唯一缺少的是填充沒有結果的行。因此,在上面的兩個解決方案中,第4,13,19行以及所有其他類似的應該在第0列和mat0_1中分別具有「無」作爲它們的值。 – thaneofcawdor
我想你需要添加reindex的範圍,如'df1 = df.dropna(axis = 1,how ='all')。groupby(level = 0).agg(lambda x:','.join(x.dropna )))。重新索引(範圍(LEN(df.index)))' – jezrael