pandas 資料透視表

2021-09-17 08:33:48 字數 861 閱讀 9083

import numpy as np

import pandas as pd

df = pd.dataframe()

df
out[38]: 

a b c d e

0 foo one small 1 2

1 foo one large 2 4

2 foo one large 2 5

3 foo two small 3 5

4 foo two small 3 6

5 bar one large 4 6

6 bar one small 5 8

7 bar two small 6 9

8 bar two large 7 9

table = pd.pivot_table(df, values='d', index=['a', 'b'],

columns=['c'], aggfunc=np.sum)

out[39]: 

c large small

a b

bar one 4.0 5.0

two 7.0 6.0

foo one 4.0 1.0

two nan 6.0

資料透視,就是對資料進行一次函式變換,將資料對映到乙個新的空間,新空間的維度是a ,b,c,新空間的值是d,可以認為是高維空間的離散點

pandas資料預處理與透視表

importpandasaspd importnumpyasnp titanic survival pd.read csv titanic train.csv 統計age 列有多少值為空 age titanic survival age age is null pd.isnull age age n...

pandas 實現excel資料透視表維度轉換

1.pivot table 實現三維資料行列轉換,將左邊資料格式轉換成右邊顯示格式 首先將寬表變成長表,用pd.melt 其中主要引數有 id vars 不需要被轉換的列名 var values 需要被轉換的列名,如果剩下的列名都需要被轉換,則可以忽略不寫 var name和value name對應...

Pandas透視表和交叉表

透視表 透視表 pivot table 是各種電子 程式和其他資料分析軟體中一種常見的資料彙總工具。它根據乙個或多個鍵對資料進行聚合,並根據行和列上得分組建將資料分配到各個矩形區域中。在python和pandas中,可以通過本章所介紹的groupby功能以及 能夠利用層次化索引的 重塑運算製作透視表...