本文概述
- 参数及说明
- 创建一个DataFrame
- 列选择
- 列添加
- 列删除
- 行选择, 添加和删除
- DataFrame函数
- 这些列可以是异构类型, 例如int, bool等。
- 它可以看作是Series结构的字典, 其中行和列都被索引了。如果是列, 则表示为” 列” ;如果是行, 则表示为” 索引” 。
index:如果未传递索引, 则默认np.arrange(n)索引用于行标签。
columns:列标签的默认语法为np.arrange(n)。如果未传递索引, 则仅显示true。
dtype:指每一列的数据类型。
copy():用于复制数据。
![Python Pandas DataFrame用法介绍](http://www.srcmini.com/wp-content/uploads/2020/03/python-pandas-dataframe.png)
文章图片
创建一个DataFrame 我们可以使用以下方式创建一个DataFrame:
- 字典
- 清单
- 脾气暴躁的ndarrrays
- 系列
下面的代码显示了如何在Pandas中创建一个空的DataFrame:
# importing the pandas libraryimport pandas as pddf = pd.DataFrame()print (df)
输出
Empty DataFrameColumns: []Index: []
说明:在上面的代码中, 首先, 我们导入了别名为pd的pandas库, 然后定义了一个名为df的变量, 该变量包含一个空的DataFrame。最后, 我们通过将df传递到打印文件中进行打印。
使用列表创建一个DataFrame:
我们可以使用list轻松在Pandas中创建一个DataFrame。
# importing the pandas libraryimport pandas as pd# a list of stringsx = ['Python', 'Pandas']# Calling DataFrame constructor on listdf = pd.DataFrame(x)print(df)
输出
00Python1Pandas
说明:在上面的代码中, 我们定义了一个名为” x” 的变量, 它由字符串值组成。正在调用DataFrame构造函数以获取列表以打印值。
从ndarrays / List的Dict创建一个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)
输出
IDDepartment0101B.Sc1102B.Tech2103M.Tech
说明:在上面的代码中, 我们定义了一个名为” info” 的字典, 该字典包含ID和Department的列表。为了打印值, 我们必须通过名为df的变量调用信息字典, 并将其作为参数传递给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)
输出
onetwoa1.01b2.02c3.03d4.04e5.05f6.06gNaN7hNaN8
说明:在上面的代码中, 名为” info” 的词典由两个具有各自索引的Series组成。为了打印值, 我们必须通过名为d1的变量调用信息字典, 并将其作为参数传递给print()。
列选择 我们可以从DataFrame中选择任何列。这是演示如何从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'])
输出
a1.0b2.0c3.0d4.0e5.0f6.0gNaNhNaNName: one, dtype: float64
说明:在上面的代码中, 名为” info” 的字典由两个具有各自索引的Series组成。后来, 我们通过变量d1调用了信息字典, 并通过将其传递到print()从DataFrame中选择了” 一个” 系列。
列添加 我们还可以将任何新列添加到现有DataFrame中。以下代码演示了如何将任何新列添加到现有DataFrame中:
# 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 object print ("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 seriesonetwothreea1.0120.0b2.0240.0c3.0360.0d4.04NaNe5.05NaNfNaN6NaNAdd new column using existing DataFrame columnsonetwothreefoura1.0120.021.0b2.0240.042.0c3.0360.063.0d4.04NaNNaNe5.05NaNNaNfNaN6NaNNaN
说明:在上面的代码中, 名为f的字典由两个具有各自索引的Series组成。后来, 我们通过变量df调用了信息字典。
为了向现有DataFrame对象添加新列, 我们传递了一个新系列, 其中包含一些有关其索引的值, 并使用print()打印了其结果。
我们可以使用现有的DataFrame添加新列。添加了” 四” 列, 该列存储两列即一列和三列相加的结果。
列删除 我们还可以从现有DataFrame中删除任何列。此代码有助于说明如何从现有DataFrame中删除该列:
# 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:onetwoa1.01b2.02cNaN3Delete the first column:twoa1b2c3Delete the another column:Empty DataFrameColumns: []Index: [a, b, c]
说明:
在上面的代码中, df变量负责调用信息字典并打印字典的全部值。我们可以使用delete或pop函数从DataFrame中删除列。
在第一种情况下, 我们使用了delete函数从DataFrame中删除” 一个” 列, 而在第二种情况下, 我们使用了pop函数从数据帧中删除了” two” 列。
行选择, 添加和删除 行选择:
我们可以随时轻松地选择, 添加或删除任何行。首先, 我们将了解行的选择。让我们看看如何使用以下不同方式选择行:
按标签选择:
我们可以通过将行标签传递给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.loc['b'])
输出
one2.0two2.0Name: b, dtype: float64
说明:在上面的代码中, 名为info的字典由两个具有各自索引的Series组成。
为了选择行, 我们将行标签传递给loc函数。
通过整数位置选择:
也可以通过将整数位置传递给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.iloc[3])
输出
one4.0two4.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])
输出
onetwoc3.03d4.04e5.05
说明:在上面的代码中, 我们为选择行定义了一个2:5的范围, 然后将其值打印在控制台上。
行加法:
我们可以使用append函数轻松地向DataFrame添加新行。它在末尾添加新行。
# 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)
输出
xy07819100111211314
说明:在上面的代码中, 我们定义了两个单独的列表, 其中包含一些行和列。这些列已使用append函数添加, 然后结果显示在控制台上。
删除行:
我们可以使用索引标签从DataFrame删除或删除任何行。如果万一标签重复, 则将删除多行。
# 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)
输出
xy16711011
说明:在上面的代码中, 我们定义了两个单独的列表, 其中包含一些行和列。
在这里, 我们定义了需要从列表中删除的行的索引标签。
DataFrame函数 DataFrame中使用了许多功能, 如下所示:
Functions | Description |
---|---|
Pandas DataFrame.append() | 将其他数据框的行添加到给定数据框的末尾。 |
Pandas DataFrame.apply() | 允许用户传递函数并将其应用于Pandas系列的每个单个值。 |
Pandas DataFrame.assign() | 将新列添加到数据框。 |
Pandas DataFrame.astype() | 将Pandas对象转换为指定的dtype.astype()函数。 |
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() | 将数值变量中的值分成” bins” 。 |
Pandas DataFrame.iterrows() | 将行迭代为(索引, 系列)对。 |
Pandas DataFrame.mean() | 返回所请求轴的值的平均值。 |
Pandas DataFrame.melt() | 将DataFrame从宽格式取消转为长格式。 |
Pandas DataFrame.merge() | 将两个数据集合并为一个。 |
Pandas DataFrame.pivot_table() | 使用诸如求和, 计数, 平均值, 最大值和最小值之类的计算来聚合数据。 |
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() | 检查数据框是否存在一种或多种情况。 |
推荐阅读
- Pandas DataFrame如何转换为Numpy数组()
- Python Pandas教程介绍
- Python Pandas系列series用法详细介绍
- ELINK编程器典型场景之多APP文件下载
- Android 使用 NYTimes Stores 缓存 network request
- Hbuilder连接安卓模拟器,调试app
- MEF and AppDomainz
- 支付宝安卓机型支付时,只有第一次可以唤醒支付窗的bug(有支付宝交易号)
- Android studio调试时遇见Install Repository and sync project的问题