文章目錄
- 重塑層次化索引
- 對于單層索引的DataFrame類對象
- stack()方法
- unstack()方法
- 對于多層索引的DataFrame類對象
- 辨析操作內層索引與外層索引的區別
- 查看多層索引對象轉換后的類型
- 軸向旋轉——pivot()方法
重塑層次化索引
Pandas中重塑層次化索引的操作主要是stack()方法和unstack()方法,前者是將數據的列“旋轉”成行,后者是將數據的行“旋轉”成列。
stack(self, level=-1, dropna=True)
上述方法中部分參數表示的含義如下:
- level:表示操作內層索引。若設為0,表示操作外層索引,默認為-1
- dropna:表示是否將旋轉后的缺失值刪除,若設為True,則表示自動過濾缺失值,設置False則相反
unstack(self, level=-1, fill_value=None)
上述方法中部分參數表示的含義如下:
- level:表示操作內層索引。若設為0,表示操作外層索引,默認為-1
- fill_value:若產生了缺失值,則可以設置這個參數用來替換NaN
對于單層索引的DataFrame類對象
stack()方法
測試對象:
df1:A B
a A0 B0
b A1 B1
代碼:
import numpy as npdf1 = pd.DataFrame({'A': ['A0', 'A1'],'B': ['B0', 'B1']},index=['a', 'b'])
df2 = pd.DataFrame(np.arange(8).reshape(2, 4),columns=[['A', 'A', 'B', 'B'],['A0', 'A1', 'B0', 'B1']])
result = df1.stack()
print("df1.stack():\n", result)
輸出結果:
df1.stack():a A A0B B0
b A A1B B1
dtype: object
從輸出結果看出,result對象具有兩層行索引。
具有兩層行索引而不再具有列索引的對象到底是DataFrame對象還是Series對象呢?
我們使用type()函數來查看result的類型,與df1做對比
type(df1):<class 'pandas.core.frame.DataFrame'>
type(result):<class 'pandas.core.series.Series'>
從輸出結果可以看出,DataFrame對象已經被轉換成了一個Series對象。
unstack()方法
嘗試將result對象重塑成df1,先將result.unstack()的結果保存在ans對象里
代碼:
ans = result.unstack()
print("ans:\n", ans)
輸出結果:
ans:A B
a A0 B0
b A1 B1
使用type()函數查看ans的類型,然后將其與df1作比較
代碼:
print("type(ans):\n", type(ans))
print("ans == df1?", ans == df1)
輸出結果:
type(ans):<class 'pandas.core.frame.DataFrame'>
ans == df1?A B
a True True
b True True
由上可知,unstack()方法是stack()方法的逆操作,可以將重塑的Series對象“恢復原樣”,轉變成原來的DataFrame對象。
對于多層索引的DataFrame類對象
測試對象:
df2:A B A0 A1 A0 A1
0 0 1 2 3
1 4 5 6 7
辨析操作內層索引與外層索引的區別
代碼:
df2 = pd.DataFrame(np.arange(8).reshape(2, 4),columns=[['A', 'A', 'B', 'B'],['A0', 'A1', 'A0', 'A1']])
result1 = df2.stack() # 操作內層索引
result2 = df2.stack(level=0) # 操作外層索引
print("df2.stack():\n", result1)
print("df2.stack(level=0):\n", result2)
輸出結果:
df2.stack():A B
0 A0 0 2A1 1 3
1 A0 4 6A1 5 7
df2.stack(level=0):A0 A1
0 A 0 1B 2 3
1 A 4 5B 6 7
查看多層索引對象轉換后的類型
代碼:
print("type(result1):\n", type(result1))
print("type(result2):\n", type(result2))
輸出結果:
type(result1):<class 'pandas.core.frame.DataFrame'>
type(result2):<class 'pandas.core.frame.DataFrame'>
對于多層索引來講,即使將外層行(列)索引或者內層行()列索引轉換為列(行)索引,也不過是多層變單層,只要行和列兩個方向仍然同時至少有單層索引存在,DataFrmae對象轉換后就仍為DataFrame對象。
軸向旋轉——pivot()方法
pivot()會根據給定的行索引或列索引重新組織一個DataFrame對象
pivot(self, index=None, columns=None, values=None)
上述方法中部分參數表示的含義如下:
- index:用于創建新DataFrame對象的行索引 。如果未設置,則使用原DataFrame對象的索引。
- columns:用于創建新DataFrame對象的列索引 。如果未設置,則使用原DataFrame對象的索引。
- values:用于填充新DataFrame對象中的值。
代碼:
df3 = pd.DataFrame({'A': ['A0', 'A0', 'A1', 'A1'],'B': ['B0', 'B1', 'B0', 'B1'],'C': ['C0', 'C1', 'C2', 'C3']})
print("df3:\n", df3)
print("df3.pivot(index='A', columns='B', values='C'):\n",
df3.pivot(index='A', columns='B', values='C'))
輸出結果:
df3:A B C
0 A0 B0 C0
1 A0 B1 C1
2 A1 B0 C2
3 A1 B1 C3
df3.pivot(index='A', columns='B', values='C'):B B0 B1
A
A0 C0 C1
A1 C2 C3