注:以下代碼均在Jupyter中運行的。
import pandas as pd 導(dǎo)入庫
df = pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
創(chuàng)建一個DataFrame
代碼 | 功能 | |
---|---|---|
1 | DataFrame() | 創(chuàng)建一個DataFrame對象 |
2 | df.values | 返回ndarray類型的對象 |
3 | df.index | 獲取行索引 |
4 | df.columns | 獲取列索引 |
5 | df.axes | 獲取行及列索引 |
6 | df.T | 行與列對調(diào) |
7 | df. info() | 打印DataFrame對象的信息 |
8 | df.head(i) | 顯示前 i 行數(shù)據(jù) |
9 | df.tail(i) | 顯示后 i 行數(shù)據(jù) |
10 | df.describe() | 查看數(shù)據(jù)按列的統(tǒng)計信息 |
DataFrame()函數(shù)的參數(shù)index的值相當(dāng)于行索引,若不手動賦值,將默認(rèn)從0開始分配。columns的值相當(dāng)于列索引,若不手動賦值,也將默認(rèn)從0開始分配。
data = { '性別':['男','女','女','男','男'], '姓名':['小明','小紅','小芳','大黑','張三'], '年齡':[20,21,25,24,29]}df = pd.DataFrame(data,index=['one','two','three','four','five'], columns=['姓名','性別','年齡','職業(yè)'])df
運行結(jié)果:
ndarray類型即numpy的 N 維數(shù)組對象,通常將DataFrame類型的數(shù)據(jù)轉(zhuǎn)換為ndarray類型的比較方便操作。如對DataFrame類型進(jìn)行切片操作需要df.iloc[ : , 1:3]這種形式,對數(shù)組類型直接X[ : , 1:3]即可。
X = df.valuesprint(type(X)) #顯示數(shù)據(jù)類型X
運行結(jié)果:
<class 'numpy.ndarray'>array([['France', 44.0, 72000.0], ['Spain', 27.0, 48000.0], ['Germany', 30.0, 54000.0], ['Spain', 38.0, 61000.0], ['Germany', 40.0, nan], ['France', 35.0, 58000.0], ['Spain', nan, 52000.0], ['France', 48.0, 79000.0], ['Germany', 50.0, 83000.0], ['France', 37.0, 67000.0]], dtype=object)
df.index
運行結(jié)果:
Index(['one', 'two', 'three', 'four', 'five'], dtype='object')
df.columns
運行結(jié)果:
Index(['姓名', '性別', '年齡', '職業(yè)'], dtype='object')
df.axes
運行結(jié)果:
[Index(['one', 'two', 'three', 'four', 'five'], dtype='object'), Index(['姓名', '性別', '年齡', '職業(yè)'], dtype='object')]
df.T
運行結(jié)果:
df.info()
運行結(jié)果:
<class 'pandas.core.frame.DataFrame'>Index: 5 entries, one to fiveData columns (total 4 columns):姓名 5 non-null object性別 5 non-null object年齡 5 non-null int64職業(yè) 0 non-null objectdtypes: int64(1), object(3)memory usage: 200.0+ bytes
df.head(2)
運行結(jié)果:
df.tail(2)
運行結(jié)果:
可顯示數(shù)據(jù)的數(shù)量、缺失值、最小最大數(shù)、平均值、分位數(shù)等信息