处理表和DataFrame之前需要先创建它们,可以用直接法、分步法、类型转换法和文件导入法等方法创建它们。[大谦MATLAB,dqmatlab点com]
直接创建表或DataFrame
使用MATLAB中的table函数可以直接创建表,使用Python中pandas包的DataFrame函数可以直接创建DataFrame。
【MATLAB】
根据工作区变量创建表,首先创建具有相同行数的工作区变量,然后用table函数创建表。
>> ID={'1';'2';'3';'4'};
>> Name={'张三';'李四';'杨二';'王五'};
>> Age=[18;19;17;21];
>> Height=[162;169;164;167];
>> Weight=[116;103;131;133];
用table函数生成表,用'RowNames'参数指定表的索引列为ID。
>> T=table(Name,Age,Height,Weight, 'RowNames',ID)
T =
4×4 table
Name Age Height Weight
________ ___ ______ ______
1 {'张三'} 18 162 116
2 {'李四'} 19 169 103
3 {'杨二'} 17 164 131
4 {'王五'} 21 167 133
本方法先创建表的内容,然后用'VariableNames'参数指定表的列名。
>> T=table({'张三';'李四';'杨二';'王五'},{'M';'F';'M';'F'},[18;19;17;25],...
[162;169;164;167],...
'VariableNames',{'Name' 'Gender' 'Age' 'Height'})
T=
Name Gender Age Height
____ ______ ___ ______
张三 'M' 18 162
李四 'F' 19 169
杨二 ' M' 17 164
王五 'F' 25 167
>> T.Properties.RowNames={'1';'2';'3';'4'}
T =
4×4 table
Name Gender Age Height
________ ______ ___ ______
1 {'张三'} {'M'} 18 162
2 {'李四'} {'F'} 19 169
3 {'杨二'} {'M'} 17 164
4 {'王五'} {'F'} 25 167
【Python】
Python中用pandas包的DataFrame函数直接创建DataFrame,函数中用一个二维列表指定DataFrame的值,用columns参数指定列名。
>>> import pandas as pd
>>> df=pd.DataFrame([['1','张三','M',18,162],['2','李四','F',19,169],['3','杨二','M',17,164],['4','王五','F',25,167]],columns=['ID','Name','Gender','Age','Height'])
>>> df
ID Name Gender Age Height
0 1 张三 M 18 162
1 2 李四 F 19 169
2 3 杨二 M 17 164
3 4 王五 F 25 167
不指定索引列时自动用0开始的累加数字给各行命名。可以用DataFrame对象的set_index方法指定索引列。下面将ID列作为索引列。
>>> df.set_index('ID')
Name Gender Age Height
ID
1 张三 M 18 162
2 李四 F 19 169
3 杨二 M 17 164
4 王五 F 25 167
创建空表或空DataFrame后添加列数据
可以用不带参数的table函数和DataFrame函数创建空表或空DataFrame,然后通过引用列变量添加列数据,最后生成表和DataFrame。
【MATLAB】
下面先创建一个空的table对象,然后用对象点引用的方式逐个指定变量名称和值。
>> Name={'张三';'李四';'杨二';'王五'};
>> Age=[18;19;17;20];
>> Height=[162;169;164;167];
>> Weight=[116;103;131;133];
>> T=table; %创建空的table对象
>> T.Name=Name;
>> T.Age=Age;
>> T.Height=Height;
>> T.Weight=Weight
T=
Name Age Height Weight
____ ___ ______ ______
'张三' 18 162 116
'李四' 19 169 103
' 杨二' 17 164 131
'王五' 20 167 133
使用表对象的Properties.RowNames属性指定索引列。
>> T.Properties.RowNames={'1';'2';'3';'4'}
T =
4×4 table
Name Gender Age Height
________ ______ ___ ______
1 {'张三'} {'M'} 18 162
2 {'李四'} {'F'} 19 169
3 {'杨二'} {'M'} 17 164
4 {'王五'} {'F'} 25 167
【Python】
下面用不带参数的DataFrame函数创建一个空的DataFrame,然后通过索引方式给每列数据设置列名和值。
>>> df=pd.DataFrame()
>>> df['ID']=['1','2','3','4']
>>> df['Name']=['张三','李四','杨二','王五']
>>> df['Age']=[18,19,17,20]
>>> df['Height']=[162,169,164,167]
>>> df['Weight']=[116,103,131,133]
>>> df
ID Name Age Height Weight
0 1 张三 18 162 116
1 2 李四 19 169 103
2 3 杨二 17 164 131
3 4 王五 20 167 133
用DataFrame对象的set_index方法将ID列设置为索引列。
>>> df.set_index('ID')
Name Age Height Weight
ID
1 张三 18 162 116
2 李四 19 169 103
3 杨二 17 164 131
4 王五 20 167 133
通过类型转换创建表或DataFrame
通过数据类型转换,可以将其他类型的数据转换为表或DataFrame。
【MATLAB】
MATLAB中,使用array2table, cell2table, struct2table和timetable2table等函数可以将普通数组、元胞数组、结构数组和时间表转换为表。
下面创建一个普通数组A,用array2table函数将它转换为表:
>> A=[92 88 93;84 75 96;87 68 89]
A =
92 88 93
84 75 96
87 68 89
用array2table函数将数组A转换为表,用VariableNames参数指定变量名称。
>> T=array2table(A,'VariableNames',{'语文' '数学' '物理'})
T =
3×3 table
语文 数学 物理
____ ____ ____
92 88 93
84 75 96
87 68 89
下面创建一个元胞数组A,用cell2table函数将它转换为表:
>> A={92 88 93;84 75 96;87 68 89}
A =
3×3 cell 数组
{[92]} {[88]} {[93]}
{[84]} {[75]} {[96]}
{[87]} {[68]} {[89]}
用cell2table函数将数组A转换为表,用VariableNames参数指定变量名称。
>> T=cell2table(A,'VariableNames',{'语文' '数学' '物理'})
T =
3×3 table
语文 数学 物理
____ ____ ____
92 88 93
84 75 96
87 68 89
【Python】
前面用直接法创建DataFrame时,实际上是用DataFrame函数将一个二维列表转换为DataFrame。下面利用二维元组创建DataFrame对象。
>>> data=((92,88,93),(84,75,96),(87,68,89)) #创建二维列表
>>> df=pd.DataFrame(data)
>>> df
0 1 2
0 92 88 93
1 84 75 96
2 87 68 89
利用字典创建DataFrame对象。字典中键值对的键表示列索引标签,值用数据区的行数据组成列表表示。
>>> data={'a':[92,88,93],'b':[84,75,96],'c':[87,68,89]}
>>> df=pd.DataFrame(data)
>>> df
a b c
0 92 84 87
1 88 75 68
2 93 96 89
利用NumPy数组创建DataFrame对象。
>>> import numpy as np
>>> data=np.array(([92,88,93], [84,75,96],[ 87,68,89]))
>>> df=pd.DataFrame(data)
>>> df
0 1 2
0 92 88 93
1 84 75 96
2 87 68 89
通过导入文件数据创建表或DataFrame
此外,还可以通过从文件导入数据来创建表或DataFrame对象,这也是最常用的一种方法。可以从Excel文件、CSV文件和文本文件等导入数据。这部分内容将在10.3节详细介绍。
描述表或DataFrame
创建表或DataFrame后,可以用函数或对象的方法对它们进行描述。描述的内容包括变量信息、数组高度、宽度、大小、值的描述性统计等。
【MATLAB】
使用summary函数可以获取表的信息,包括表中各变量的数据类型、数组大小等,对于数值型变量,给出数据的最小值、最大值和中值。
下面创建一个表,然后用summary函数获取该表的信息。
>> T=table({'张三';'李四';'杨二';'王五'},{'M';'F';'M';'F'},[18;19;17;25],...
[162;169;164;167],logical([0;0;0;1]),...
'VariableNames',{'Name' 'Gender' 'Age' 'Height' 'Married'})
>> summary(T)
Variables:
Name: 4×1 cell array of character vectors
Gender: 4×1 cell array of character vectors
Age: 4×1 double
Values:
Min 17
Median 18.5
Max 25
Height: 4×1 double
Values:
Min 162
Median 165.5
Max 169
Married: 4×1 logical
Values:
True 1
False 3
使用height函数获取表的行数,用width函数获取表的变量数。
>> height(T)
ans =
4
>> width(T)
ans =
5
用size函数获取表的大小。
>> size(T)
ans =
4 5
使用head函数和tail函数获取给定表的前N行和后N行。
下面创建一个有10行数据的表,用head函数获取表的前5行,用tail函数获取表的后5行。
>> ID={'1';'2';'3';'4';'5';'6';'7';'8';'9';'10'};
>> Name={'张三';'李四';'杨二';'王五';'韩英';'赵军';'余成';'黄丽';'谭梅';'苏云'};
>> Age=[18;19;17;21;19;22;20;18;17;23];
>> Height=[162;169;164;167;173;168;171;167;175;170];
>> Weight=[116;103;131;133;93;121;113;101;98;130];
>> T=table(Name,Age,Height,Weight, 'RowNames',ID);
>> head(T,5) %获取前5行
ans =
5×4 table
Name Age Height Weight
________ ___ ______ ______
1 {'张三'} 18 162 116
2 {'李四'} 19 169 103
3 {'杨二'} 17 164 131
4 {'王五'} 21 167 133
5 {'韩英'} 19 173 93
>> tail(T,5) %获取最后5行
ans =
5×4 table
Name Age Height Weight
________ ___ ______ ______
6 {'赵军'} 22 168 121
7 {'余成'} 20 171 113
8 {'黄丽'} 18 167 101
9 {'谭梅'} 17 175 98
10 {'苏云'} 23 170 130
【Python】
创建DataFrame对象以后,可以使用info, describe, dtypes, shape等一系列属性和方法对它进行描述。下面首先创建一个DataFrame对象df。
>>> data=[[1,2,3],[4,5,6],[7,8,9]]
>>> df=pd.DataFrame(data,index=['a','b','c'],columns=['A','B','C'])
>>> df
A B C
a 1 2 3
b 4 5 6
c 7 8 9
用info方法获取df的信息。
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 3 entries, a to c
Data columns(total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 A 3 non-null int64
1 B 3 non-null int64
2 C 3 non-null int64
dtypes: int64(3)
memory usage: 96.0+ bytes
使用info方法获取的DataFrame对象信息包括对象的类型、行索引和列索引信息、每列数据的列标签、非缺失值个数和数据类型、占用内存大小等。
用dtypes属性获取df每列数据的类型。
>>> df.dtypes
A int64
B int64
C int64
dtype: object
用shape属性获取df的行数和列数,用元组给出。
>>> df.shape
(3, 3)
用len函数获取df的行数和列数。
>>> len(df) #行数
3
>>> len(df.columns) #列数
3
用index属性获取df的行索引标签。
>>> df.index
Index(['a', 'b', 'c'], dtype='object')
用columns属性获取df的列索引标签。
>>> df.columns
Index(['A', 'B', 'C'], dtype='object')
用values属性获取df的值。
>>> df.values
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=int64)
用head方法获取前n行数据,默认时n=5。
>>> df.head(2)
A B C
a 1 2 3
b 4 5 6
用tail方法获取后n行数据,默认时n=5。
>>> df.tail(2)
A B C
b 4 5 6
c 7 8 9
用describe方法获取df每列数据的描述统计量,包括数据个数、均值、标准差、最小值、25%分位数、中值、75%分位数、最大值等。
>>> df.describe()
A B C
count 3.0 3.0 3.0
mean 4.0 5.0 6.0
std 3.0 3.0 3.0
min 1.0 2.0 3.0
25% 2.5 3.5 4.5
50% 4.0 5.0 6.0
75% 5.5 6.5 7.5
max 7.0 8.0 9.0