Python – 使用Iris数据集的Pandas基础知识

Python – 使用Iris数据集的Pandas基础知识

Python语言是最流行的编程语言之一,因为它比其他语言更具活力。Python是一种简单的高级语言,是一种用于通用编程的开源语言。它有许多开源库,Pandas就是其中之一。Pandas是一个强大、快速、灵活的开源库,用于数据分析和数据帧/数据集的操作。Pandas可以用来读写不同格式的数据集,如CSV(逗号分隔值)、txt、xls(Microsoft Excel)等。
在这篇文章中,你将了解Python中Pandas的各种功能,以及如何使用它来进行实践。
安装:
因此,如果你是练习Pandas的新手,那么首先你应该在你的系统上安装Pandas
转到命令提示符,以管理员身份运行它。确保你有互联网连接,以便下载并安装在你的系统上。
然后输入” pip install pandas “,再按回车键。

Python - 使用Iris数据集的Pandas基础知识

从这里下载数据集 “Iris.csv”。
Iris数据集是数据科学的Hello World,所以如果你已经开始了你在数据科学和机器学习方面的职业生涯,你将在这个著名的数据集上练习基本的ML算法。鸢尾花数据集包含五列数据,如花瓣长度、花瓣宽度、萼片长度、萼片宽度和物种类型。
鸢尾花是一种开花植物,研究人员测量了不同鸢尾花的各种特征,并以数字方式记录下来。

Python - 使用Iris数据集的Pandas基础知识

开始使用Pandas:
代码。将pandas导入我们的代码中作为pd.使用。

import pandas as pd

代码。读取数据集 “Iris.csv “

data = pd.read_csv("your downloaded dataset location ")

Python - 使用Iris数据集的Pandas基础知识

代码:显示数据集最上面的行和它们的列
函数head()将显示数据集的最上面几行,这个函数的默认值是5,也就是说,当没有给它参数时,它将显示最上面的5行。

data.head()

输出:

Python - 使用Iris数据集的Pandas基础知识

随机显示行数
在sample()函数中,它也会根据给出的参数显示行,但它会随机显示行。

data.sample(10)

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:显示列的数量和列的名称
column()函数以列表形式打印出数据集的所有列。

data.columns

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:显示数据集的形状
数据集的形状是指打印该特定数据集的总行数或条目数和总列数或特征。

#The first one is the number of rows and 
# the other one is the number of columns.
data.shape

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:显示整个数据集

print(data)

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:划分行数
切片是指如果你想打印或在一个特定的线组上工作,即从第10行到第20行。

#data[start:end]
#start is inclusive whereas end is exclusive
print(data[10:21])
# it will print the rows from 10 to 20.
  
# you can also save it in a variable for further use in analysis
sliced_data=data[10:21]
print(sliced_data)

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:只显示特定列
在任何数据集中,有时需要只对特定的特征或列进行处理,因此我们可以通过以下代码来实现。

#here in the case of Iris dataset
#we will save it in a another variable named "specific_data"
  
specific_data=data[["Id","Species"]]
#data[["column_name1","column_name2","column_name3"]]
  
#now we will print the first 10 columns of the specific_data dataframe.
print(specific_data.head(10))

输出:

Python - 使用Iris数据集的Pandas基础知识

过滤:使用 “iloc “和 “loc “函数显示特定的行:

loc “函数使用行的索引名称来显示数据集的特定行。
iloc “函数使用行的索引整数,它给出了关于行的完整信息。
代码:

#here we will use iloc
  
data.iloc[5]
#it will display records only with species "Iris-setosa".
data.loc[data["Species"] == "Iris-setosa"]

输出:

Python - 使用Iris数据集的Pandas基础知识

iloc()[/caption]

Python - 使用Iris数据集的Pandas基础知识

loc()

代码。使用 “value_counts() “计算唯一值的计数数。
value_counts()函数,对一个特定的实例或数据发生的次数进行计数。

#In this dataset we will work on the Species column, it will count number of times a particular species has occurred.
data["Species"].value_counts()
#it will display in descending order.

输出:

Python - 使用Iris数据集的Pandas基础知识

计算某一列的总和、平均数和模式
我们还可以计算任何整数列的总和、平均值和模式,正如我在下面的代码中所做的那样。

# data["column_name"].sum()
  
sum_data = data["SepalLengthCm"].sum()
mean_data = data["SepalLengthCm"].mean()
median_data = data["SepalLengthCm"].median()
  
print("Sum:",sum_data, "\nMean:", mean_data, "\nMedian:",median_data)

输出:

Python - 使用Iris数据集的Pandas基础知识

代码。从一列中提取最小值和最大值
识别最小和最大的整数,从一个特定的列或行也可以在一个数据集中完成。

min_data=data["SepalLengthCm"].min()
max_data=data["SepalLengthCm"].max()
  
print("Minimum:",min_data, "\nMaximum:", max_data)

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:向数据集添加一列
如果想在我们的数据集中添加一个新的列,因为我们正在做任何计算或从数据集中提取一些信息,如果你想把它保存为一个新的列。这可以通过下面的代码来完成,在这种情况下,我们已经添加了所有列的整数值。

# For example, if we want to add a column let say "total_values", 
# that means if you want to add all the integer value of that particular
# row and get total answer in the new column "total_values".
# first we will extract the columns which have integer values.
cols = data.columns
  
# it will print the list of column names.
print(cols)
  
# we will take that columns which have integer values.
cols = cols[1:5]
  
# we will save it in the new dataframe variable
data1 = data[cols]
  
# now adding new column "total_values" to dataframe data.
data["total_values"]=data1[cols].sum(axis=1)
  
# here axis=1 means you are working in rows, 
# whereas axis=0 means you are working in columns.

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:重命名列
重命名我们的列名也可以在python pandas库中实现。我们使用了 rename() 函数,在这里我们创建了一个字典 “newcols “来更新我们的新列名。下面的代码说明了这一点。

newcols={
"Id":"id",
"SepalLengthCm":"sepallength"
"SepalWidthCm":"sepalwidth"}
  
data.rename(columns=newcols,inplace=True)
  
print(data.head())

输出:

Python - 使用Iris数据集的Pandas基础知识

格式和风格:
通过使用Dataframe.style函数,可以将条件格式化应用于你的数据框架。样式用于可视化你的数据,而最方便的可视化你的数据集的方式是以表格的形式。
在这里,我们将强调每一行和每一列的最小和最大。

#this is an example of rendering a datagram, 
which is not visualised by any styles. 
data.style

输出:

Python - 使用Iris数据集的Pandas基础知识

现在我们将使用Styler.apply函数突出显示最大和最小的列、行和整个数据框架的情况。Styler.apply函数根据关键字参数axis传递数据框架的每一列或每一行。对于按列计算,使用axis=0,按行计算,使用axis=1,而对于整个表格,使用axis=None。

# we will here print only the top 10 rows of the dataset, 
# if you want to see the result of the whole dataset remove 
#.head(10) from the below code
  
data.head(10).style.highlight_max(color='lightgreen', axis=0)
  
data.head(10).style.highlight_max(color='lightgreen', axis=1)
  
data.head(10).style.highlight_max(color='lightgreen', axis=None)

输出:

Python - 使用Iris数据集的Pandas基础知识

for axis=0

Python - 使用Iris数据集的Pandas基础知识

for axis=1

Python - 使用Iris数据集的Pandas基础知识

for axis=None

代码:清理和检测缺失值
在这个数据集中,我们现在将尝试寻找缺失值,即NaN,这可能是由于几种原因造成的。

data.isnull()
#if there is data is missing, it will display True else False.

输出:

Python - 使用Iris数据集的Pandas基础知识

isnull()

代码:总结缺失的价值
我们将显示每一列中存在多少缺失值。

data.isnull.sum()

输出:

Python - 使用Iris数据集的Pandas基础知识

热图:导入海宝
热图是一种数据可视化技术,用于分析数据集的二维色彩。基本上,它显示了数据集中所有数字变量之间的相关性。热图是Seaborn库的一个属性。
代码:

import seaborn as sns
  
iris = sns.load_dataset("iris")
sns.heatmap(iris.corr(),camp = "YlGnBu", linecolor = 'white', linewidths = 1)

输出:

Python - 使用Iris数据集的Pandas基础知识

代码:使用整数格式为每个单元格的数值做注释

sns.heatmap(iris.corr(),camp = "YlGnBu", linecolor = 'white', linewidths = 1, annot = True )

输出:

Python - 使用Iris数据集的Pandas基础知识

热图,注释=True

Pandas数据框架的相关性:
Pandas相关性用于确定数据集中所有列的成对相关性。在dataframe.corr()中,缺失值被排除,非数字列也被忽略。
代码:

data.corr(method='pearson')

输出:

Python - 使用Iris数据集的Pandas基础知识

data.corr()

输出的数据框架可以看作是对于任何单元格,行变量与列变量的相关性是该单元格的值。变量与自身的相关性为1。为此,所有对角线的数值都是1.00。
Multivariate Analysis:
对子图是用来直观地显示每一类列变量之间的关系。它仅由一行代码实现,如下所示。
代码:

g = sns.pairplot(data,hue="Species")

输出:

Python - 使用Iris数据集的Pandas基础知识

变量 “物种 “的配对图,以使其更容易理解。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程