我正在嘗試將以下具有多個二進制變數的資料集轉換為交叉表。
df = pd.DataFrame({'colour_blue': [1, 0, 1], 'colour_green': [0, 1, 1],
'colour_red': [1, 1, 0], 'emotion_happy': [1, 1, 1],'emotion_angry': [0, 1, 1], 'emotion_sad': [0, 0, 1]})
進入以下交叉表,但我沒有運氣
| 情感 | 藍色 | 綠色 | 紅色的 |
|---|---|---|---|
| 快樂的 | 2 | 2 | 2 |
| 生氣的 | 1 | 2 | 1 |
| 傷心 | 1 | 1 | 0 |
uj5u.com熱心網友回復:
這是矩陣乘法:
df.filter(like='emotion').T @ df.filter(like='colour')
輸出:
colour_blue colour_green colour_red
emotion_happy 2 2 2
emotion_angry 1 2 1
emotion_sad 1 1 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/456780.html
下一篇:計算每個索引熊貓的間隔
