Python之数据重塑——【stack()方法和unstack()方法、pivot()方法】
文章目錄
- 重塑層次化索引
- 對于單層索引的DataFrame類對象
- stack()方法
- unstack()方法
- 對于多層索引的DataFrame類對象
- 辨析操作內層索引與外層索引的區別
- 查看多層索引對象轉換后的類型
- 軸向旋轉——pivot()方法
重塑層次化索引
Pandas中重塑層次化索引的操作主要是stack()方法和unstack()方法,前者是將數據的列“旋轉”成行,后者是將數據的行“旋轉”成列。
stack(self, level=-1, dropna=True)
上述方法中部分參數表示的含義如下:
unstack(self, level=-1, fill_value=None)
上述方法中部分參數表示的含義如下:
對于單層索引的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)
上述方法中部分參數表示的含義如下:
代碼:
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總結
以上是生活随笔為你收集整理的Python之数据重塑——【stack()方法和unstack()方法、pivot()方法】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript onkeydown
- 下一篇: python 编辑数学公式_Jupyte