DataFrame

DataFrame 是 Pandas 中最重要的数据结构之一,它类似于【电子表格或数据库表】,可以存储和处理二维标记数据, 也可以看做是【由Series组成的字典】(共用同一个索引)。DataFrame由按一定顺序排列的多列数据组成。 设计初衷是将Series的使用场景从一维拓展到二维。DataFrame既有行索引,也有列索引。

行索引:index列索引:columns值:values(numpy的二维数组)

DataFrame的创建

1.字典创建

DataFrame以字典的键作为每一【列】的名称,以字典的值(一个数组)作为每一列。 DataFrame会自动加上每一行的索引(和Series一样); 同Series一样,若传入的列与字典的键不匹配,则相应的值为NaN

## import numpy as np

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张三948373107李四111416494王五551254231赵六5196578田七787388

2.从列表创建

data = [['Alice', 25, 'New York'],

['Bob', 30, 'Los Angeles'],

['Charlie', 35, 'Chicago']]

df1 = DataFrame(data, columns=['Name', 'Age', 'City'])

df1 # 自动加上每一行的索引

NameAgeCity0Alice25New York1Bob30Los Angeles2Charlie35Chicago

3.NumPy 数组创建

data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

df2 = DataFrame(data, columns=['A', 'B', 'C'])

df2

ABC012314562789

DataFrame的属性

df

语文数学英语python张三948373107李四111416494王五551254231赵六5196578田七787388

1.常用属性shape,size,index, columns, values, dtypes

shape 属性是一个元组,表示 DataFrame 的维度,即行数和列数。size 属性表示 DataFrame 中元素的总数,即行数乘以列数。index 属性表示 DataFrame 的行索引,即每行的标签。columns 属性表示 DataFrame 的列名,即每列的标签。values 属性返回 DataFrame 中的实际数据,以 NumPy 数组的形式呈现。dtypes 属性显示 DataFrame 中每列的数据类型。

df.shape # (5, 4)

df.size # 20

df.index # Index(['张三', '李四', '王五', '赵六', '田七'], dtype='object')

df.columns # Index(['语文', '数学', '英语', 'python'], dtype='object')

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()

"""

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()

语文数学英语pythoncount5.0000005.0000005.00005.000000mean77.80000083.60000038.400063.600000std25.50882230.80259731.245842.335564min51.00000041.0000005.00008.00000025%55.00000073.0000008.000031.00000050%78.00000083.00000042.000078.00000075%94.00000096.00000064.000094.000000max111.000000125.00000073.0000107.000000

DataFrame的索引与切片

1.对列进行索引

- 通过类似字典的方式

- 通过属性的方式

可以将DataFrame的列获取为一个Series。返回的Series拥有原DataFrame相同的索引,且name属性也已经设置好了,就是相应的列名。

df

语文数学英语python张三948373107李四111416494王五551254231赵六5196578田七787388

# 类似字典的key的方式. dict[key]

# dataframe中取出一列数据, 返回的是Series.

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['理综'] = np.random.randint(0, 300, size=(5,))

df

语文数学英语python理综张三94837310713李四111416494117王五551254231203赵六519657823田七787388168

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] # 返回的是DataFrame

语文数学英语python理综张三94837310713李四111416494117王五551254231203

3.对元素索引的方法

使用列索引 - 使用行索引(iloc[3,1]相当于两个参数;iloc[[3,3]] 里面的[3,3]看做一个参数)

df['数学']['张三'] # 83 效率低,需要两次索引操作;先找列索引'数学',在找行索引'张三'

df['数学'].loc['张三'] # 83; 效率低,需要两次索引操作

# 推荐写法. 查找元素的时候先行后列; 效率高,只进行一次索引

df.loc['张三', '数学'] # 83

# 隐式写法. 也是先行后列

df.iloc[0, 1] # 83

4.切片

# 行切片, 显式切片是全闭区间.

df.loc['张三': '王五']

语文数学英语python理综张三94837310713李四111416494117王五551254231203

# 隐式行切片, 左闭右开.

df.iloc[0:2]

语文数学英语python理综张三94837310713李四111416494117

# 列切片, 直接中括号进行切片. 是对行进行的切片.

# df['语文': '英语'] 异常! 因为行没有对应的标签

# 对列进行切片的时候, 也要先行后列.

df.loc[:, '语文': '英语'] # : 表示拿所有行

语文数学英语张三948373李四1114164王五5512542赵六51965田七78738

df.iloc[:, 0: 2] # 左闭右开

语文数学张三9483李四11141王五55125赵六5196田七7873

df[['语文', '英语']]

语文英语张三9473李四11164王五5542赵六515田七788

# 布尔索引: 选择满足条件的行数据

df[df['语文'] > 90]

语文数学英语python理综张三94837310713李四111416494117

参考阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: