DataFrame
DataFrame 是 Pandas 中最重要的数据结构之一,它类似于【电子表格或数据库表】,可以存储和处理二维标记数据,
也可以看做是【由Series组成的字典】(共用同一个索引)。DataFrame由按一定顺序排列的多列数据组成。
设计初衷是将Series的使用场景从一维拓展到二维。DataFrame既有行索引,也有列索引。
- 行索引:index
- 列索引:columns
- 值:values(numpy的二维数组)
DataFrame的创建
1.字典创建
DataFrame以字典的键作为每一【列】的名称,以字典的值(一个数组)作为每一列。
DataFrame会自动加上每一行的索引(和Series一样);
同Series一样,若传入的列与字典的键不匹配,则相应的值为NaN
from pandas import DataFrame
df = DataFrame(data={'语文': np.random.randint(0, 150, size=(5,)),
'数学': np.random.randint(0, 150, size=(5,)),
'英语': np.random.randint(0, 150, size=(5,)),
'python': np.random.randint(0, 150, size=(5,))},
index=['张三', '李四', '王五', '赵六', '田七'])
df
| 语文 | 数学 | 英语 | python |
---|
张三 | 94 | 83 | 73 | 107 |
---|
李四 | 111 | 41 | 64 | 94 |
---|
王五 | 55 | 125 | 42 | 31 |
---|
赵六 | 51 | 96 | 5 | 78 |
---|
田七 | 78 | 73 | 8 | 8 |
---|
2.从列表创建
data = [['Alice', 25, 'New York'],
['Bob', 30, 'Los Angeles'],
['Charlie', 35, 'Chicago']]
df1 = DataFrame(data, columns=['Name', 'Age', 'City'])
df1
| Name | Age | City |
---|
0 | Alice | 25 | New York |
---|
1 | Bob | 30 | Los Angeles |
---|
2 | Charlie | 35 | Chicago |
---|
3.NumPy 数组创建
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df2 = DataFrame(data, columns=['A', 'B', 'C'])
df2
DataFrame的属性
df
| 语文 | 数学 | 英语 | python |
---|
张三 | 94 | 83 | 73 | 107 |
---|
李四 | 111 | 41 | 64 | 94 |
---|
王五 | 55 | 125 | 42 | 31 |
---|
赵六 | 51 | 96 | 5 | 78 |
---|
田七 | 78 | 73 | 8 | 8 |
---|
1.常用属性shape,size,index, columns, values, dtypes
- shape 属性是一个元组,表示 DataFrame 的维度,即行数和列数。
- size 属性表示 DataFrame 中元素的总数,即行数乘以列数。
- index 属性表示 DataFrame 的行索引,即每行的标签。
- columns 属性表示 DataFrame 的列名,即每列的标签。
- values 属性返回 DataFrame 中的实际数据,以 NumPy 数组的形式呈现。
- dtypes 属性显示 DataFrame 中每列的数据类型。
df.shape
df.size
df.index
df.columns
df.values
"""
array([[ 94, 83, 73, 107],
[111, 41, 64, 94],
[ 55, 125, 42, 31],
[ 51, 96, 5, 78],
[ 78, 73, 8, 8]])
"""
df.dtypes
"""
语文 int32
数学 int32
英语 int32
python int32
dtype: object
"""
2. 信息统计和描述info(), describe()
- info() 方法提供了关于 DataFrame 的详细信息,包括每列的非空值数量、数据类型等.
- describe() 方法生成关于 DataFrame 中数值列的统计摘要,包括均值、标准差、最小值、最大值等。
df.info()
"""
<class 'pandas.core.frame.DataFrame'>
Index: 5 entries, 张三 to 田七
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 语文 5 non-null int32
1 数学 5 non-null int32
2 英语 5 non-null int32
3 python 5 non-null int32
dtypes: int32(4)
memory usage: 120.0+ bytes
"""
df.describe()
| 语文 | 数学 | 英语 | python |
---|
count | 5.000000 | 5.000000 | 5.0000 | 5.000000 |
---|
mean | 77.800000 | 83.600000 | 38.4000 | 63.600000 |
---|
std | 25.508822 | 30.802597 | 31.2458 | 42.335564 |
---|
min | 51.000000 | 41.000000 | 5.0000 | 8.000000 |
---|
25% | 55.000000 | 73.000000 | 8.0000 | 31.000000 |
---|
50% | 78.000000 | 83.000000 | 42.0000 | 78.000000 |
---|
75% | 94.000000 | 96.000000 | 64.0000 | 94.000000 |
---|
max | 111.000000 | 125.000000 | 73.0000 | 107.000000 |
---|
DataFrame的索引与切片
1.对列进行索引
- 通过类似字典的方式
- 通过属性的方式
可以将DataFrame的列获取为一个Series。返回的Series拥有原DataFrame相同的索引,且name属性也已经设置好了,就是相应的列名。
df
| 语文 | 数学 | 英语 | python |
---|
张三 | 94 | 83 | 73 | 107 |
---|
李四 | 111 | 41 | 64 | 94 |
---|
王五 | 55 | 125 | 42 | 31 |
---|
赵六 | 51 | 96 | 5 | 78 |
---|
田七 | 78 | 73 | 8 | 8 |
---|
df['语文']
"""
张三 94
李四 111
王五 55
赵六 51
田七 78
Name: 语文, dtype: int32
"""
df.数学
"""
张三 83
李四 41
王五 125
赵六 96
田七 73
Name: 数学, dtype: int32
"""
df['理综'] = np.random.randint(0, 300, size=(5,))
df
| 语文 | 数学 | 英语 | python | 理综 |
---|
张三 | 94 | 83 | 73 | 107 | 13 |
---|
李四 | 111 | 41 | 64 | 94 | 117 |
---|
王五 | 55 | 125 | 42 | 31 | 203 |
---|
赵六 | 51 | 96 | 5 | 78 | 23 |
---|
田七 | 78 | 73 | 8 | 8 | 168 |
---|
2. 对行进行索引
- 使用.loc[] 加index来进行行索引
- 使用.iloc[] 加整数来进行行索引
同样返回一个Series,index为原来的columns。
df.loc['张三']
"""
语文 94
数学 83
英语 73
python 107
理综 13
Name: 张三, dtype: int32
"""
df.iloc[2]
"""
语文 55
数学 125
英语 42
python 31
理综 203
Name: 王五, dtype: int32
"""
df.iloc[0:3]
| 语文 | 数学 | 英语 | python | 理综 |
---|
张三 | 94 | 83 | 73 | 107 | 13 |
---|
李四 | 111 | 41 | 64 | 94 | 117 |
---|
王五 | 55 | 125 | 42 | 31 | 203 |
---|
3.对元素索引的方法
- 使用列索引 - 使用行索引(iloc[3,1]相当于两个参数;iloc[[3,3]] 里面的[3,3]看做一个参数)
df['数学']['张三']
df['数学'].loc['张三']
df.loc['张三', '数学']
df.iloc[0, 1]
4.切片
df.loc['张三': '王五']
| 语文 | 数学 | 英语 | python | 理综 |
---|
张三 | 94 | 83 | 73 | 107 | 13 |
---|
李四 | 111 | 41 | 64 | 94 | 117 |
---|
王五 | 55 | 125 | 42 | 31 | 203 |
---|
df.iloc[0:2]
| 语文 | 数学 | 英语 | python | 理综 |
---|
张三 | 94 | 83 | 73 | 107 | 13 |
---|
李四 | 111 | 41 | 64 | 94 | 117 |
---|
df.loc[:, '语文': '英语']
| 语文 | 数学 | 英语 |
---|
张三 | 94 | 83 | 73 |
---|
李四 | 111 | 41 | 64 |
---|
王五 | 55 | 125 | 42 |
---|
赵六 | 51 | 96 | 5 |
---|
田七 | 78 | 73 | 8 |
---|
df.iloc[:, 0: 2]
| 语文 | 数学 |
---|
张三 | 94 | 83 |
---|
李四 | 111 | 41 |
---|
王五 | 55 | 125 |
---|
赵六 | 51 | 96 |
---|
田七 | 78 | 73 |
---|
df[['语文', '英语']]
| 语文 | 英语 |
---|
张三 | 94 | 73 |
---|
李四 | 111 | 64 |
---|
王五 | 55 | 42 |
---|
赵六 | 51 | 5 |
---|
田七 | 78 | 8 |
---|
df[df['语文'] > 90]
| 语文 | 数学 | 英语 | python | 理综 |
---|
张三 | 94 | 83 | 73 | 107 | 13 |
---|
李四 | 111 | 41 | 64 | 94 | 117 |
---|