Pandas 教程-Python Pandas DataFrame
Pandas DataFrame(数据框)是一种广泛使用的数据结构,它与带有标签轴(行和列)的二维数组一起工作。DataFrame 被定义为存储具有两个不同索引的数据的标准方式,即行索引和列索引。它具有以下属性:
列可以是异构类型,如整数、布尔值等。
它可以被看作是一种 Series 结构的字典,其中行和列都被索引。在列的情况下,它表示为“columns”,在行的情况下表示为“index”。
参数和描述:
data: 它由不同形式组成,如 ndarray、series、map、constants、lists、array。
index: 如果没有传递索引,则默认使用 np.arrange(n) 索引作为行标签。
columns: 列标签的默认语法是 np.arrange(n)。如果没有传递索引,则仅显示 true。
dtype: 指的是每列的数据类型。
copy(): 用于复制数据。
专属福利 👉点击领取:最全Python资料合集
创建 DataFrame
我们可以使用以下方式创建 DataFrame:
字典
列表
Numpy 数组
Series
创建一个空的 DataFrame
# importing the pandas libraryimport pandas as pddf = pd.DataFrame()print (df)
Empty DataFrameColumns: []Index: []
说明: 在上面的代码中,首先我们导入了 pandas 库并使用别名 pd,然后定义了一个名为 df 的变量,其中包含一个空的 DataFrame。最后,我们通过将 df 传递给 print 来打印它。
使用列表创建 DataFrame:
# importing the pandas libraryimport pandas as pd# a list of stringsx = ['Python', 'Pandas']# Calling DataFrame constructor on listdf = pd.DataFrame(x)print(df)
00 Python1 Pandas
说明: 在上面的代码中,我们定义了一个名为 "x" 的变量,其中包含字符串值。调用了列表的 DataFrame 构造函数以打印值。
使用字典的 ndarrays/列表创建 DataFrame
# importing the pandas libraryimport pandas as pdinfo = {'ID' :[101, 102, 103],'Department' :['B.Sc','B.Tech','M.Tech',]}df = pd.DataFrame(info)print (df)
ID Department0 101 B.Sc1 102 B.Tech2 103 M.Tech
说明: 在上面的代码中,我们定义了一个名为 "info" 的字典,其中包含 ID 和 Department 的列表。为了打印值,我们必须通过一个名为 df 的变量调用 info 字典,并将其作为参数传递给 print()。
使用 Series 的字典创建 DataFrame:
# importing the pandas libraryimport pandas as pdinfo = {'one' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']),'two' : pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])}d1 = pd.DataFrame(info)print (d1)
one twoa 1.0 1b 2.0 2c 3.0 3d 4.0 4e 5.0 5f 6.0 6g NaN 7h NaN 8
说明: 在上面的代码中,一个名为 "info" 的字典包含两个具有各自索引的 Series。为了打印值,我们必须通过一个名为 d1 的变量调用 info 字典,并将其作为参数传递给 print()。
列选择
# importing the pandas libraryimport pandas as pdinfo = {'one' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']),'two' : pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])}d1 = pd.DataFrame(info)print (d1 ['one'])
a 1.0b 2.0c 3.0d 4.0e 5.0f 6.0g NaNh NaNName: one, dtype: float64
说明: 在上面的代码中,一个名为 "info" 的字典包含两个具有各自索引的 Series。随后,我们通过一个名为 d1 的变量调用 info 字典,并通过将其传递给 print() 选择了 "one" Series。
列添加
# importing the pandas libraryimport pandas as pdinfo = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}df = pd.DataFrame(info)# Add a new column to an existing DataFrame objectprint ("Add new column by passing series")df['three']=pd.Series([20,40,60],index=['a','b','c'])print (df)print ("Add new column using existing DataFrame columns")df['four']=df['one']+df['three']print (df)
Add new column by passing seriesone two threea 1.0 1 20.0b 2.0 2 40.0c 3.0 3 60.0d 4.0 4 NaNe 5.0 5 NaNf NaN 6 NaNAdd new column using existing DataFrame columnsone two three foura 1.0 1 20.0 21.0b 2.0 2 40.0 42.0c 3.0 3 60.0 63.0d 4.0 4 NaN NaNe 5.0 5 NaN NaNf NaN 6 NaN NaN
说明: 在上面的代码中,一个名为 f 的字典包含两个具有各自索引的 Series。随后,我们通过一个名为 df 的变量调用 info 字典。
要将新列添加到现有的 DataFrame 对象中,我们传递了一个包含一些值的新 series,并使用 print() 打印了其结果。
我们可以使用现有 DataFrame 添加新列。已添加了“four”列,该列存储两列的加法结果,即 one 和 three。
列删除:
# importing the pandas libraryimport pandas as pdinfo = {'one' : pd.Series([1, 2], index= ['a', 'b']),'two' : pd.Series([1, 2, 3], index=['a', 'b', 'c'])}df = pd.DataFrame(info)print ("The DataFrame:")print (df)# using del functionprint ("Delete the first column:")del df['one']print (df)# using pop functionprint ("Delete the another column:")df.pop('two')print (df)
The DataFrame:one twoa 1.0 1b 2.0 2c NaN 3Delete the first column:twoa 1b 2c 3Delete the another column:Empty DataFrameColumns: []Index: [a, b, c]
说明:
在上面的代码中,df 变量负责调用 info 字典并打印字典的所有值。我们可以使用 delete 或 pop 函数从 DataFrame 中删除列。
在第一种情况中,我们使用 delete 函数从 DataFrame 中删除了 "one" 列,而在第二种情况中,我们使用 pop 函数从 DataFrame 中删除了 "two" 列。
行选择、添加和删除
行选择:
我们可以随时轻松选择、添加或删除任何行。首先,我们将了解行选择。让我们看看如何使用不同的方式选择行,如下所示:
通过标签选择:
# importing the pandas libraryimport pandas as pdinfo = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}df = pd.DataFrame(info)print (df.loc['b'])
one 2.0two 2.0Name: b, dtype: float64
说明: 在上面的代码中,一个名为 "info" 的字典包含两个具有各自索引的 Series。
要选择一行,我们将行标签传递给 loc 函数。
通过整数位置选择:
# importing the pandas libraryimport pandas as pdinfo = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}df = pd.DataFrame(info)print (df.iloc[3])
one 4.0two 4.0Name: d, dtype: float64
说明: 在上面的代码中,我们定义了一个名为 "info" 的字典,其中包含两个具有各自索引的 Series。
要选择一行,我们将整数位置传递给 iloc 函数。
切片行
# importing the pandas libraryimport pandas as pdinfo = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']),'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}df = pd.DataFrame(info)print (df[2:5])
one twoc 3.0 3d 4.0 4e 5.0 5
说明: 在上面的代码中,我们定义了一个范围从 2:5 的区间以选择行,然后在控制台上打印其值。
添加行:
# importing the pandas libraryimport pandas as pdd = pd.DataFrame([[7, 8], [9, 10]], columns = ['x','y'])d2 = pd.DataFrame([[11, 12], [13, 14]], columns = ['x','y'])d = d.append(d2)print (d)
x y0 7 81 9 100 11 121 13 14
说明: 在上面的代码中,我们定义了两个分别包含一些行和列的列表。然后,使用 append 函数添加了这些列,然后在控制台上显示了结果。
删除行:
# importing the pandas libraryimport pandas as pda_info = pd.DataFrame([[4, 5], [6, 7]], columns = ['x','y'])b_info = pd.DataFrame([[8, 9], [10, 11]], columns = ['x','y'])a_info = a_info.append(b_info)# Drop rows with label 0a_info = a_info.drop(0)
x y1 6 71 10 11
说明: 在上面的代码中,我们定义了两个分别包含一些行和列的列表。
这里,我们定义了要从列表中删除的行的索引标签。
DataFrame 函数
| 函数 | 描述 |
|---|---|
| Pandas DataFrame.append() | 将其他 dataframe 的行添加到给定 dataframe 的末尾。 |
| Pandas DataFrame.apply() | 允许用户传递一个函数,并将其应用于 Pandas series 的每个单个值。 |
| Pandas DataFrame.assign() | 向 dataframe 中添加新列。 |
| Pandas DataFrame.astype() | 将 Pandas 对象转换为指定的数据类型。 |
| Pandas DataFrame.concat() | 在 DataFrame 上沿轴执行连接操作。 |
| Pandas DataFrame.count() | 计算每列或每行的非 NA 单元格数。 |
| Pandas DataFrame.describe() | 计算一些统计数据,如 Series 或 DataFrame 的数字值的百分位数、均值和标准差。 |
| Pandas DataFrame.drop_duplicates() | 从 DataFrame 中删除重复值。 |
| Pandas DataFrame.groupby() | 将数据拆分成各种组。 |
| Pandas DataFrame.head() | 基于位置返回对象的前 n 行。 |
| Pandas DataFrame.hist() | 将数值变量中的值分割为“箱子”。 |
| Pandas DataFrame.iterrows() | 遍历行作为(索引,系列)对。 |
| Pandas DataFrame.mean() | 返回请求轴的值的平均值。 |
| Pandas DataFrame.melt() | 将 DataFrame 从宽格式解除成长格式。 |
| Pandas DataFrame.merge() | 将两个数据集合并为一个。 |
| Pandas DataFrame.pivot_table() | 使用诸如 Sum、Count、Average、Max 和 Min 等计算聚合数据。 |
| Pandas DataFrame.query() | 过滤数据帧。 |
| Pandas DataFrame.sample() | 随机选择数据帧的行和列。 |
| Pandas DataFrame.shift() | 移动列或从数据帧中减去前一行值的列值。 |
| Pandas DataFrame.sort() | 对数据帧进行排序。 |
| Pandas DataFrame.sum() | 返回用户请求轴的值的总和。 |
| Pandas DataFrame.to_excel() | 将数据帧导出到 Excel 文件。 |
| Pandas DataFrame.transpose() | 转置数据帧的索引和列。 |
| Pandas DataFrame.where() | 检查数据帧是否满足一个或多个条件。 |
热门推荐