熊猫数据集
If you haven’t read the first article then it is advised that you go through that before continuing with this article. You can find that article here. So far we have learned how to access data in different ways. Now we will learn how to analyze data to get better understanding and then to manipulate it.
如果您还没有阅读第一篇文章,那么建议您在继续阅读本文之前先进行阅读。 您可以在这里找到该文章。 到目前为止,我们已经学习了如何以不同的方式访问数据。 现在,我们将学习如何分析数据以获得更好的理解,然后进行操作。
So just to give overview, in this article we are going to learn
因此,为了概述,在本文中我们将学习
- How to summarize data? 如何汇总数据?
- How to manipulate data? 如何处理数据?
汇总数据 (Summarizing Data)
We have been using different methods to view data which is helpful if we wanted to summarize data for specific rows or columns. However, pandas provide simpler methods to view data.
我们一直在使用不同的方法来查看数据,这对于希望汇总特定行或列的数据很有帮助。 但是,熊猫提供了更简单的方法来查看数据。
If we want to see few data items to understand what kind of data is present in dataset pandas provide methods like head() and tail(). head() provides few rows from the top, by default it provide first five rows and tail(), as you might have guessed, provide rows from bottom of dataset. You can also specify a number to show how many rows you want to display as head(n) or tail(n).
如果我们希望看到很少的数据项以了解数据集中存在的数据类型,熊猫可以提供head()和tail()之类的方法 。 head()从顶部提供几行,默认情况下,它提供前五行,而您可能已经猜到了tail(),从数据集的底部提供行。 您还可以指定一个数字,以显示要显示为head(n)或tail(n)的行数。
>> print(titanic_data.head())output : PassengerId Survived Pclass .......
0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3
[5 rows x 12 columns]>> print(titanic_data.tail())output : PassengerId Survived Pcl Name .........
886 887 0 2 Montvila, Rev. Juozas
887 888 1 1 Graham, Miss. Margaret Edith
888 889 0 3 Johnston, Miss. Catherine Hele..
889 890 1 1 Behr, Mr. Karl Howell
890 891 0 3 Dooley, Mr. Patrick[5 rows x 12 columns]>> print(titanic_data.tail(3))output : PassengerId Survived Pcl Name .........
888 889 0 3 Johnston, Miss. Catherine Hele..
889 890 1 1 Behr, Mr. Karl Howell
890 891 0 3 Dooley, Mr. Patrick[3 rows x 12 columns]
We can also display the data statistics of our dataset. We use describe()
method to get statistics for every column. We can also get statistic for a specific column.
我们还可以显示数据集的数据统计信息。 我们使用describe()
方法获取每一列的统计信息。 我们还可以获取特定列的统计信息。
>> print(titanic_data.describe())output : PassengerId Survived Pclass Age SibSp ...
count 891.000000 891.000000 891.000000 714.000000 891.000000
mean 446.000000 0.383838 2.308642 29.699118 0.523008
std 257.353842 0.486592 0.836071 14.526497 1.102743
min 1.000000 0.000000 1.000000 0.420000 0.000000
25% 223.500000 0.000000 2.000000 20.125000 0.000000
50% 446.000000 0.000000 3.000000 28.000000 0.000000
75% 668.500000 1.000000 3.000000 38.000000 1.000000
max 891.000000 1.000000 3.000000 80.000000 8.000000>> print(titanic_data.Fare.decribe())output :count 891.000000
mean 32.204208
std 49.693429
min 0.000000
25% 7.910400
50% 14.454200
75% 31.000000
max 512.329200
Name: Fare, dtype: float64
Remember, it only return statistical data for numerical columns. It displays statistics like count i.e number of data points in that column, mean of data points, standard deviation and so on. If you do not want to see this whole stats then you can also call on these parameters individually.
请记住,它仅返回数字列的统计数据。 它显示统计信息,例如计数,即该列中数据点的数量,数据点的平均值,标准偏差等。 如果您不希望看到整个统计信息,则也可以单独调用这些参数。
>> print(titanic_data.Fare.mean())output :32.204208
处理数据 (Manipulating Data)
map()
: It is use to manipulate data in a Series. We usemap()
method on a columns ofdataset. map()
takes a function as parameter and that function takes a data point from specified column asparameter. map()
iterates over all data points of a column and then returns new updated series.map()
:用于处理系列中的数据。 我们在dataset. map()
的列上使用map()
方法dataset. map()
dataset. map()
将函数作为参数,而该函数将指定列中的数据点作为parameter. map()
parameter. map()
遍历列的所有数据点,然后返回新的更新的系列。apply()
: It is used to manipulate data in a Dataframe. It behaves almost same asmap()
but it takes Series (row or column) as parameter to given function which in return provide updated Series and finally after all iteration of Series,apply()
returns a new Dataframe.apply()
:用于处理数据帧中的数据。 它的行为几乎与map()
相同,但是它将Series(行或列)作为给定函数的参数,该函数提供更新的Series,最后在Series的所有迭代之后,apply()
返回一个新的Dataframe。
# Here we define a function which will be used as parameter to map()>> def updateUsingMap(data_point):
'''
This function make data more readable by changing
Survived columns values to Yes if 1
and No if 0
Parameters
----------
data_point : int Returns
-------
data_point : string '''
updated_data = ''
if(data_point==0):
updated_data = "No"
else:
updated_data = "Yes"
return updated_data>> print(titatic_data.Survived.map(updateUsingMap))output :0 No
1 Yes
2 Yes
3 Yes
4 No
.....
Name: Survived, Length: 891, dtype: object# Here we define a function which will be used as parameter to apply()def updateUsingApply(row):
'''
This function make data more readable by changing
Survived columns values to Yes if 1
and No if 0
Parameters
----------
row : Series Returns
-------
row : Series '''if(row.Survived==0):
row.Survived = "No"
else:
row.Survived = "Yes"
return row
>> print(titatic_data.apply(updateUsingMap,axis = 'columns'))output : PassengerId Survived Pclass .......
0 1 No 3
1 2 Yes 1
2 3 Yes 3
3 4 Yes 1
4 5 No 3
.. ... ... ...
[891 rows x 12 columns]
One thing needs to be clear here that these methods do not manipulate or change original data. It creates a new Series or Dataframe. As you noticed that we used another parameter in apply()
method that is axis. It is used to specify that we want to change data along the rows. In order to change data along the columns we would have supplied value of axis as index.
需要明确的一点是,这些方法不会操纵或更改原始数据。 它创建一个新的系列或数据框。 您已经注意到,我们在apply()
方法中使用了另一个参数axis。 它用于指定我们要沿行更改数据。 为了沿列更改数据,我们将提供轴的值作为索引。
I think it is enough for this article. Let this information sink in and then we can start with next article to explore few more methods in Pandas till then keep practicing. Happy Coding! 😄
我认为这篇文章就足够了。 让这些信息沉入其中,然后我们可以从下一篇文章开始,探索熊猫中的其他方法,然后继续练习。 编码愉快! 😄
普通英语的Python (Python In Plain English)
Did you know that we have three publications and a YouTube channel? Find links to everything at plainenglish.io!
您知道我们有三个出版物和一个YouTube频道吗? 在plainenglish.io上找到所有内容的链接!
熊猫数据集