
DataFrame
DataFrame 是由多種類型的列構(gòu)成的二維標簽數(shù)據(jù)結(jié)構(gòu),類似于 Excel 、SQL 表,或 Series 對象構(gòu)成的字典。DataFrame 是最常用的 Pandas 對象,與 Series 一樣,DataFrame 支持多種類型的輸入數(shù)據(jù):
- 一維 ndarray、列表、字典、Series 字典
- 二維 numpy.ndarray
- 結(jié)構(gòu)多維數(shù)組或記錄多維數(shù)組
SeriesDataFrame
除了數(shù)據(jù),還可以有選擇地傳遞 index(行標簽)和 columns(列標簽)參數(shù)。傳遞了索引或列,就可以確保生成的 DataFrame 里包含索引或列。Series 字典加上指定索引時,會丟棄與傳遞的索引不匹配的所有數(shù)據(jù)。
沒有傳遞軸標簽時,按常規(guī)依據(jù)輸入數(shù)據(jù)進行構(gòu)建。
Python > = 3.6,且 Pandas > = 0.23,數(shù)據(jù)是字典,且未指定
columns參數(shù)時,DataFrame的列按字典的插入順序排序。Python < 3.6 或 Pandas < 0.23,且未指定
columns參數(shù)時,DataFrame的列按字典鍵的字母排序。
用 Series 字典或字典生成 DataFrame
生成的索引是每個 Series 索引的并集。先把嵌套字典轉(zhuǎn)換為 Series。如果沒有指定列,DataFrame 的列就是字典鍵的有序列表。
In [37]: d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
....: 'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
....:
In [38]: df = pd.DataFrame(d)
In [39]: df
Out[39]:
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
In [40]: pd.DataFrame(d, index=['d', 'b', 'a'])
Out[40]:
one two
d NaN 4.0
b 2.0 2.0
a 1.0 1.0
In [41]: pd.DataFrame(d, index=['d', 'b', 'a'], columns=['two', 'three'])
Out[41]:
two three
d 4.0 NaN
b 2.0 NaN
a 1.0 NaN
index 和 columns 屬性分別用于訪問行、列標簽:
指定列與數(shù)據(jù)字典一起傳遞時,傳遞的列會覆蓋字典的鍵。
In [42]: df.index
Out[42]: Index(['a', 'b', 'c', 'd'], dtype='object')
In [43]: df.columns
Out[43]: Index(['one', 'two'], dtype='object')
用多維數(shù)組字典、列表字典生成 DataFrame
多維數(shù)組的長度必須相同。如果傳遞了索引參數(shù),index 的長度必須與數(shù)組一致。如果沒有傳遞索引參數(shù),生成的結(jié)果是 range(n),n 為數(shù)組長度。
In [44]: d = {'one': [1., 2., 3., 4.],
....: 'two': [4., 3., 2., 1.]}
....:
In [45]: pd.DataFrame(d)
Out[45]:
one two
0 1.0 4.0
1 2.0 3.0
2 3.0 2.0
3 4.0 1.0
In [46]: pd.DataFrame(d, index=['a', 'b', 'c', 'd'])
Out[46]:
one two
a 1.0 4.0
b 2.0 3.0
c 3.0 2.0
d 4.0 1.0
用結(jié)構(gòu)多維數(shù)組或記錄多維數(shù)組生成 DataFrame
本例與數(shù)組字典的操作方式相同。
In [47]: data = np.zeros((2, ), dtype=[('A', 'i4'), ('B', 'f4'), ('C', 'a10')])
In [48]: data[:] = [(1, 2., 'Hello'), (2, 3., "World")]
In [49]: pd.DataFrame(data)
Out[49]:
A B C
0 1 2.0 b'Hello'
1 2 3.0 b'World'
In [50]: pd.DataFrame(data, index=['first', 'second'])
Out[50]:
A B C
first 1 2.0 b'Hello'
second 2 3.0 b'World'
In [51]: pd.DataFrame(data, columns=['C', 'A', 'B'])
Out[51]:
C A B
0 b'Hello' 1 2.0
1 b'World' 2 3.0
DataFrame 的運作方式與 NumPy 二維數(shù)組不同。
用列表字典生成 DataFrame
In [52]: data2 = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}]
In [53]: pd.DataFrame(data2)
Out[53]:
a b c
0 1 2 NaN
1 5 10 20.0
In [54]: pd.DataFrame(data2, index=['first', 'second'])
Out[54]:
a b c
first 1 2 NaN
second 5 10 20.0
In [55]: pd.DataFrame(data2, columns=['a', 'b'])
Out[55]:
a b
0 1 2
1 5 10
用元組字典生成 DataFrame
元組字典可以自動創(chuàng)建多層索引 DataFrame。
In [56]: pd.DataFrame({('a', 'b'): {('A', 'B'): 1, ('A', 'C'): 2},
....: ('a', 'a'): {('A', 'C'): 3, ('A', 'B'): 4},
....: ('a', 'c'): {('A', 'B'): 5, ('A', 'C'): 6},
....: ('b', 'a'): {('A', 'C'): 7, ('A', 'B'): 8},
....: ('b', 'b'): {('A', 'D'): 9, ('A', 'B'): 10}})
....:
Out[56]:
a b
b a c a b
A B 1.0 4.0 5.0 8.0 10.0
C 2.0 3.0 6.0 7.0 NaN
D NaN NaN NaN NaN 9.0
用 Series 創(chuàng)建 DataFrame
生成的 DataFrame 繼承了輸入的 Series 的索引,如果沒有指定列名,默認列名是輸入 Series 的名稱。
缺失數(shù)據(jù)
更多內(nèi)容,詳見缺失數(shù)據(jù) 。DataFrame 里的缺失值用 np.nan 表示。DataFrame 構(gòu)建器以 numpy.MaskedArray 為參數(shù)時 ,被屏蔽的條目為缺失數(shù)據(jù)。
備選構(gòu)建器
DataFrame.from_dict
DataFrame.from_dict 接收字典組成的字典或數(shù)組序列字典,并生成 DataFrame。除了 orient 參數(shù)默認為 columns,本構(gòu)建器的操作與 DataFrame 構(gòu)建器類似。把 orient 參數(shù)設(shè)置為 'index', 即可把字典的鍵作為行標簽。
In [57]: pd.DataFrame.from_dict(dict([('A', [1, 2, 3]), ('B', [4, 5, 6])]))
Out[57]:
A B
0 1 4
1 2 5
2 3 6
orient='index' 時,鍵是行標簽。本例還傳遞了列名:
In [58]: pd.DataFrame.from_dict(dict([('A', [1, 2, 3]), ('B', [4, 5, 6])]),
....: orient='index', columns=['one', 'two', 'three'])
....:
Out[58]:
one two three
A 1 2 3
B 4 5 6
DataFrame.from_records
DataFrame.from_records 構(gòu)建器支持元組列表或結(jié)構(gòu)數(shù)據(jù)類型(dtype)的多維數(shù)組。本構(gòu)建器與 DataFrame 構(gòu)建器類似,只不過生成的 DataFrame 索引是結(jié)構(gòu)數(shù)據(jù)類型指定的字段。例如:
In [59]: data
Out[59]:
array([(1, 2., b'Hello'), (2, 3., b'World')],
dtype=[('A', '<i4'), ('B', '<f4'), ('C', 'S10')])
In [60]: pd.DataFrame.from_records(data, index='C')
Out[60]:
A B
C
b'Hello' 1 2.0
b'World' 2 3.0

