数据帧(DataFrame)是二维数据结构,即数据以行和列的表格方式排列。python
数据帧(DataFrame)的功能特色:shell
结构体数组
假设要建立一个包含学生数据的数据帧。参考如下图示 -数据结构
能够将上图表视为SQL表或电子表格数据表示。app
pandas中的DataFrame
可使用如下构造函数建立 -函数
pandas.DataFrame( data, index, columns, dtype, copy)
参数 | 描述 |
---|---|
data |
数据采起各类形式,如:ndarray ,series ,map ,lists ,dict ,constant 和另外一个DataFrame 。 |
index |
对于行标签,要用于结果帧的索引是可选缺省值np.arrange(n) ,若是没有传递索引值。 |
columns |
对于列标签,可选的默认语法是 - np.arange(n) 。 这只有在没有索引传递的状况下才是这样。 |
dtype |
每列的数据类型。 |
copy |
若是默认值为False ,则此命令(或任何它)用于复制数据。 |
Pandas数据帧(DataFrame)可使用各类输入建立,如 -spa
在本章的后续章节中,咱们将看到如何使用这些输入建立数据帧(DataFrame)。code
建立基本数据帧是空数据帧。blog
import pandas as pd df = pd.DataFrame() print(df)
输出结果:索引
Empty DataFrame Columns: [] Index: []
可使用单个列表或列表列表建立数据帧(DataFrame)。
实例-1
import pandas as pd data = [1,2,3,4,5] df = pd.DataFrame(data) print(df)
输出结果:
0 0 1 1 2 2 3 3 4 4 5
实例-2
import pandas as pd data = [['Alex',10],['Bob',12],['Clarke',13]] df = pd.DataFrame(data,columns=['Name','Age']) print(df)
输出结果:
Name Age 0 Alex 10 1 Bob 12 2 Clarke 13
实例-3
import pandas as pd data = [['Alex',10],['Bob',12],['Clarke',13]] df = pd.DataFrame(data,columns=['Name','Age'],dtype=float) print(df)
输出结果:
Name Age 0 Alex 10.0 1 Bob 12.0 2 Clarke 13.0
注意 - 能够观察到,
dtype
参数将Age
列的类型更改成浮点。
全部的ndarrays
必须具备相同的长度。若是传递了索引(index
),则索引的长度应等于数组的长度。
若是没有传递索引,则默认状况下,索引将为range(n)
,其中n
为数组长度。
实例-1
import pandas as pd data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]} df = pd.DataFrame(data) print(df)
输出结果:
Age Name 0 28 Tom 1 34 Jack 2 29 Steve 3 42 Ricky
注 - 观察值
0
,1
,2
,3
。它们是分配给每一个使用函数range(n)
的默认索引。
示例-2
使用数组建立一个索引的数据帧(DataFrame)。
import pandas as pd data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]} df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4']) print(df)
输出结果:
Age Name rank1 28 Tom rank2 34 Jack rank3 29 Steve rank4 42 Ricky
注意 -
index
参数为每行分配一个索引。
字典列表可做为输入数据,用来建立数据帧(DataFrame),字典键默认为列名。
实例-1
如下示例显示如何经过传递字典列表来建立数据帧(DataFrame)。
import pandas as pd data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] df = pd.DataFrame(data) print(df)
输出结果:
a b c 0 1 2 NaN 1 5 10 20.0
注意 - 观察到,NaN(不是数字)被附加在缺失的区域。
示例-2
如下示例显示如何经过传递字典列表和行索引来建立数据帧(DataFrame)。
import pandas as pd data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] df = pd.DataFrame(data, index=['first', 'second']) print(df)
输出结果:
a b c first 1 2 NaN second 5 10 20.0
实例-3
如下示例显示如何使用字典,行索引和列索引列表建立数据帧(DataFrame)。
import pandas as pd data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] #With two column indices, values same as dictionary keys df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b']) #With two column indices with one index with other name df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1']) print(df1) print('\n') print(df2)
输出结果:
a b first 1 2 second 5 10 a b1 first 1 NaN second 5 NaN
注意 - 观察,
df2
使用字典键之外的列索引建立DataFrame
; 所以,附加了NaN到位置上。 而df1
是使用列索引建立的,与字典键相同,因此也附加了NaN。
字典的系列能够传递以造成一个DataFrame。 所获得的索引是全部系列索引的并集。
示例
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print(df)
输出结果:
one two a 1.0 1 b 2.0 2 c 3.0 3 d NaN 4
注意 - 对于第一个系列,观察到没有传递标签
'd'
,但在结果中,对于d
标签,附加了NaN。
经过列名,来选择列
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print(df ['one'])
输出结果:
a 1.0 b 2.0 c 3.0 d NaN Name: one, dtype: float64
像字典赋值同样直接添加。
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) # Adding a new column to an existing DataFrame object with column label by passing new series print ("Adding a new column by passing as Series:") df['three']=pd.Series([10,20,30],index=['a','b','c']) print(df) print('\n') print ("Adding a new column using the existing columns in DataFrame:") df['four']=df['one']+df['three'] print(df)
输出结果:
Adding a new column by passing as Series: one two three a 1.0 1 10.0 b 2.0 2 20.0 c 3.0 3 30.0 d NaN 4 NaN Adding a new column using the existing columns in DataFrame: one two three four a 1.0 1 10.0 11.0 b 2.0 2 20.0 22.0 c 3.0 3 30.0 33.0 d NaN 4 NaN NaN
列能够删除或弹出;
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']), 'three' : pd.Series([10,20,30], index=['a','b','c'])} df = pd.DataFrame(d) print ("Our dataframe is:") print(df) print('\n') # using del function print ("Deleting the first column using DEL function:") del df['one'] print(df) print('\n') # using pop function print ("Deleting column using POP function:") df.pop('two') print(df)
输出结果 -
Our dataframe is: one three two a 1.0 10.0 1 b 2.0 20.0 2 c 3.0 30.0 3 d NaN NaN 4 Deleting the first column using DEL function: three two a 10.0 1 b 20.0 2 c 30.0 3 d NaN 4 Deleting column using POP function: three a 10.0 b 20.0 c 30.0 d NaN
标签选择
能够经过将行标签传递给loc()
函数来选择行。参考如下示例代码 -
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print(df.loc['b'])
输出结果:
one 2.0 two 2.0 Name: b, dtype: float64
结果是一系列标签做为DataFrame
的列名称。 并且,系列的名称是检索的标签。
按整数位置选择
能够经过将整数位置传递给iloc()
函数来选择行。参考如下示例代码 -
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print(df.iloc[2])
输出结果:
one 3.0 two 3.0 Name: c, dtype: float64
可使用:
运算符选择多行。参考如下示例代码 -
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print(df[2:4])
输出结果:
one two c 3.0 3 d NaN 4
附加行
使用append()
函数将新行添加到DataFrame。 此功能将附加行结束。
import pandas as pd df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b']) df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b']) df = df.append(df2) print(df)
输出结果:
a b 0 1 2 1 3 4 0 5 6 1 7 8
删除行
使用索引标签从DataFrame中删除或删除行。 若是标签重复,则会删除多行。
import pandas as pd df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b']) print(df) print('\n') df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b']) print(df2) print('\n') df = df.append(df2) print(df) print('\n') # Drop rows with label 0 df = df.drop(0) print(df)
输出结果:
a b
0 1 2
1 3 4
a b
0 5 6
1 7 8
a b
0 1 2
1 3 4
0 5 6
1 7 8
a b
1 3 4
1 7 8
在上面的例子中,一共有两行被删除,由于这两行包含相同的标签0
。