数据分析工作坊系列

与熊猫的数据分析

讲师:Conor Murphy, Databricks的数据科学顾问

康纳·墨菲(Conor Murphy)在花了四年时间利用数据在发展中国家进行更有影响力的人道主义干预后,转向了科技行业,重点是商业发展。他为扶轮基金会管理数百万美元的奖助金组合,专注于发展及分析经济发展计划的影响测量、评估计划参与及将学术研究转化为机构政策。

他曾担任多种职位,包括Galvanize的数据科学研究生项目的教师职位,一些初创公司的首席数据科学家和顾问,以及Databricks的数据科学家和教育家。在数据之外,康纳是一个狂热的跳伞爱好者,他总是寻找极客的方法来量化他在自由落体中的时间。

车间细节

本研讨会是我们为有抱负的数据科学家提供的数据分析入门系列研讨会的第二部分。

这个研讨会是关于pandas的,这是一个用于数据分析和操作的强大的开源Python包。在本研讨会中,您将学习如何读取数据、计算汇总统计信息、检查数据分布、执行基本的数据清理和转换,以及绘制简单的可视化图。我们将使用约翰霍普金斯大学系统科学与工程中心(CSSE)新型冠状病毒(COVID-19) (https://github.com/CSSEGISandData/COVID-19)发布的数据。建议具备Python基础经验。

你需要:注册社区版在这里并获得研讨会演示材料和样本笔记本在这里

虽然不需要任何准备工作,但我们建议您了解基本的python知识。请观看第一部分,Python介绍以了解Python。

视频记录

为有抱负的数据科学家介绍数据分析

这是我们的第二部分,关于熊猫的数据分析。

下周我们会有第三个关于机器学习的研讨会时间是下周三,22号上午十点和本周的时间一致。然后我们来看看第一部分,这是上周录制的。以上就是Python和数据库介绍,视频可以通过我们的YouTube频道观看。这是一个缩短的链接。我也会把它放在聊天中,让每个人都能轻松访问。

所以我想对所有加入我们的人说一句,

即将举行的网上活动

了解我们所有内容、我们所有的研讨会、我们对不同工程师、开发人员的采访以及Tech Talks的最新信息的最佳方式是加入我的Meetup小组中的数据+ AI。在后续材料中有一个链接,我也会把它链接起来。所以现在不用担心把它抄下来。还有,我知道有很多人从YouTube上加入我们,这真的很了不起,也很令人兴奋。请在这里注明订阅我们的频道,并确保你打开了通知。所以我们要做的是在我发布即将到来的直播之前至少有几天的时间,无论是技术谈话还是研讨会。如果你想在YouTube上看的话,我会把这个视频发布出来,这样你就可以给自己设置一个提醒。

工作坊资源链接

这里有两个链接供大家准备。如果您通过我们的Meetup组加入,您昨天应该已经收到我的消息,鼓励您提前注册社区版。所以,当我给你们所有人介绍的时候,看看这些链接,这就是你们今天需要为研讨会做的准备。我想我们还有点时间,如果有什么问题要和助教一起解决问题,但是要注册社区版。如果你们上周参加了我们的研讨会,我想你们中的很多人可能已经知道了。这是今天研讨会的两个链接。在我让我们的助教和Conor老师自我介绍之前,我要说明一下。

见见你的导师和助教

这是有记录的,所以不用担心以后会不会再来上课。我们很高兴你也得到了所有的资源。如果你通过Zoom加入,你会在24小时内收到一封电子邮件,里面有你需要的所有链接。视频录制的链接,GitHub的链接,到Meetup组的链接,还有YouTube频道,这样你就可以看到即将发布的所有内容,也可以看看我们即将发布的内容。

有些人使用聊天工具只是为了告诉他们你来自哪所学校,你在哪里拨号,这非常棒。如果我们能把问题导向问答功能,那就太完美了。我们会让助教回答问题。如果最后还有时间,Conor, Brooke和Chenglyn也会回答一些他们认为对整个小组都有帮助的问题。废话不多说,Brooke和Chenglyn,请你们简单介绍一下然后我们就开始吧。

-当然,我叫布鲁克·维尼格。我是机器学习实践的负责人,我非常兴奋能和我的团队成员Conor和Chenglyn一起在这个网络研讨会上工作。

-嗨,我是程琳。我和布鲁克一起在机器学习实践团队工作。我是数据科学顾问。你们中的一些人可能在之前的网络研讨会上认识我,但我做过(听不清)培训,也帮助客户设计解决方案。

-太棒了,谢谢。现在我们把它交给Conor,他是我们今天的老师。康纳,把它拿走。-太好了,谢谢。我叫康纳·墨菲。我与Chenglyn和Brooke在我们的机器学习实践团队中密切合作。这是一个混合团队,我们是机器学习和数据科学项目的顾问。我们也会做一些指导。所以,很好。让我们开始吧。 Let me go ahead and share my screen and so let’s see. Let me just make sure this is the right screen.

好了,希望大家都能看清楚。在开始之前,我们首先要确保能够访问Databricks。你可以去右上角的www.neidfyre.com。你可以点击试试Databricks,你可以在这里注册,如果你上周上过我们的课,你可以去community。cloud。Databricks。com,你可以在这里访问。我给大家一分钟时间开始吧。一旦你登录到那里,继续,并在聊天中留下一个简短的消息,这样我们就知道你已经获得了访问权限。

如果你在这个过程中遇到了任何挑战,也可以在Q$A频道中发表。

真酷,看来有几个人进来了。

太好了,我们再等几分钟。

所以看起来很多人都能使用它。我们继续往下讲如果你们在这过程中有任何挑战,可以在问答环节中提出我们可以让助教在整个过程中帮助你们。但是登录后,您应该会看到一个类似这样的学习页面。如果你上周听过我们的课,这看起来应该很眼熟。如果你上周没和我们在一起,那也完全没问题。这就是Databricks环境。在其他方面,我们允许在针对集群执行代码的笔记本中使用钩子。这些集群是我们为你创建的资源这样你就有地方运行你的代码了。这段代码不是在你的笔记本电脑或本地电脑上运行,而是在云中运行。所以你会在亚马逊的基础设施上运行这个。 So on the left hand side of the screen, you can go ahead and click the clusters tab. And then you can click, create cluster. If were with us last week, you might see a cluster that’s already here that just needs to be restarted. You can click, create cluster here. You can give it a name, you can call it whatever you want. I’m just gonna call it this pandas. And then I’m gonna click, create cluster. The rest of these configuration should be totally fine.

我可以点击,创建一个集群,像这样。一会儿它就会出现在屏幕上。我给你们一点时间来了解一下。

然后在聊天中告诉我们你是否能够启动一个集群。

因此,似乎有人问他们是否可以使用之前创建的集群。是的,您可以使用该集群。你只需要重新启动它。

- [Brooke]抱歉,实际上Databricks CE上没有启用。它在企业版中启用。这一个你必须从头开始重新创建。-明白了,谢谢你,布鲁克-[布鲁克]是的。

我需要注册我的信用卡吗?不,你不需要注册你的信用卡。因此,这是一个完全免费的产品,只允许您在Databricks环境中运行代码。

很好,看起来你们很多人都已经开始运行了。下一步就是导入我们今天课上要用到的笔记本。如果你点击左上角的home键,然后点击这个下拉框,你可以点击代码。这样我们就可以导入那个笔记本了。一旦你点击导入,点击从URL然后,我把这个链接粘贴到这里,你可以从这个端点导入。

我要把它粘贴到这里,点击导入。一会儿我们会看到一个新的笔记本出现在这里。所以它应该是这样的。

再说一次,我给你们一点时间来处理。

导入笔记本并启动集群后,请给我们发送通知。

看起来有些人,他们的集群还在等待。这完全没问题。所以这些应该很快就能开始了。所以通常需要,我不知道,可能需要一到三分钟左右来启动集群。如果我点击这个下拉菜单,我就会看到这个集群我可以通过这里的绿灯看到它正在运行。如果这里有个转轮,那就意味着它还在启动。所以这是一个免费的资源。这是实际要运行的代码我们将从笔记本上执行这些代码。所以我们有大约15又1 / 4 g。我们有两个核心,剩下的这些配置只是我们正在运行的软件版本。 So from that dropdown menu, I can just click on that cluster and then I will have attached this notebook to that cluster. So now I can execute code on that cluster.

太酷了,让我们开始吧。今天我们将介绍用熊猫进行数据分析。所以我们要开始了,我们要解释为什么熊猫很重要。谈一谈它是什么和它的历史。然后我们会从更高的层次上讲,如何使用这个包。所以我们将使用COVID-19数据集。这是可以用的。还有一个事后实验室。所以你可以自己做分析,深入到你的国家,你的州或你的地区,了解这些趋势是什么。

最后,我们也会做一些可视化。我还想给你一些事后可以使用的资源。这里有几个链接。首先,这是一张小抄。这是熊猫创造者提供的小抄。

当你试图记住如何做某事时,这是一个非常有效的记忆工具。为了让你们了解基本语法,一些数据上的基本操作,一些过滤操作,如何处理缺失数据,诸如此类。这个小抄非常有用。我也在这里链接了熊猫文档。熊猫的文档看起来是这样的。如果你遇到什么问题,这将是你的主要参考。这里有一些有用的入门指南,你可以随时查看。你可以单独做这些。这里也有一些视频资源,但这应该会给你更多的资源,让你更成功。如果你也需要看API参考文档,你可以在这里看到。 And so this will give you a lot more detail on each and every one of the functions or methods that we’ll be using in the next hour.

很好,我们开始吧。

在我的课堂上或在我的职业生涯中,我教过相当多的编程介绍。我总是喜欢谈论的一种方式就是疯狂的大图景。首先,我们来看看为什么要用熊猫?人类之所以是如此成功的动物,其中一个原因就是我们对工具的使用。电脑可以说是我们所创造的最强大的工具之一。所以如果你真的能写代码,你就能释放这些工具的全部力量。我总是觉得这非常令人兴奋。所以当我第一次开始教授编程入门课程的时候,我喜欢和大家分享这一点。这就是为什么写代码如此有效的原因。但是为什么我们要专门用熊猫呢? And so pandas is a data munging library available in the Python environment or within the Python programming language. And so more and more industry trends are showing that data is leading the decision making. And so you can use similar tools. You can use something like Excel instead. However, pandas allows certain benefits beyond something like spreadsheeting tools. So for instance, if you wanna automate your analysis so that reruns on any new data each day, you can find ways to do that within Excel, but it’s generally a little bit challenging. And so code-based tools such as pandas allow you to do that. If you want to, say, build a code base and share it with your colleagues, you can do that using Python and using pandas as well. If you want more robust analyses that feed your decision making processes of your business, pandas allows you to do that, and if you wanna do machine learning as well. And so the next course in the series, part three, next week, we’ll be focusing more on machine learning and it’ll give you an introduction to how to do that. And so if you’re a data scientist and if you’re a data scientist using Python, you’re likely going to be using pandas quite a bit. And so pandas is going to allow us to manipulate the data that we need so that we can feed it into machine learning models so that we can do visualizations, so that we can calculate reports, so that we can do all of those things. And so pandas is absolutely an essential library for anything that you do as a data analyst or data scientist within Python. So that’s pandas. So at a high level, what is pandas? So it’s an open source library. So open source means that anybody has access to it. You can use it free of charge and you can see what the underlying source code is. So basically it’s a bunch of code that somebody else wrote for you and you can use that in order to manipulate data. So it’s used for data analysis, data manipulation, and it’s built on top of Python.

它开始于2008年,并在2009年完全开源。bob下载地址我不会一一介绍使用熊猫的这些不同的好处,但你可以看到,熊猫有很多不同的功能或不同的特征,这些功能或特征非常有用。我们主要会与这个DataFrame对象交互。这样就能得到你想要的数据格式,对吧?这是一种表格数据格式,有点类似于电子表格,但它能让你拥有比电子表格更强大的功能。因此,它优化了读写数据的工具。它有处理丢失数据的工具。你可以重塑它,你可以旋转,你可以索引,你可以做各种非常有趣,非常有用的事情。还有一些时间序列功能还有一些可视化功能。我们今天会讲到很多这些特征。 And then if you’re interested, there’s a book here that you can check out as well. This is, just open it here. So this is “Python for Data Analysis.” This was written by West McKinney. West McKinney is the original creator of pandas. And there were a ton of helpful exercises and helpful walkthroughs here. So if you ever get stuck or if you want a deeper dive, I’d definitely recommend checking out this book.

很酷,让我们从COVID-19数据集开始。这个数据集将通过Databricks数据集提供给你。这是我们为你们托管的一些不同的数据集这样你们就可以更容易地测试不同的代码,查看不同的特性。我可以使用这个% sh ls,然后这里有这个包。

如果你熟悉Jupyter笔记本,你可能会更熟悉感叹号当你在做这样的命令时。让我看一下这个文件系统中到底有什么。如果我想浏览这个文件系统,我可以看到在Databricks数据集中,有一个COVID文件夹然后这里有很多不同的子目录。这是我们要用到的子目录我想导航一下,看看里面到底有什么。我可以粘贴到这里仔细看看里面到底是什么。在CSSE COVID-19数据下,你可以看到我们将使用这些每日报告。如果我在这个目录上调用% sh ls,你会看到这里有很多不同的CSV文件。它从1月22日左右开始直到4月11日。这里我们将主要使用这个CSV文件。虽然最后我们将把所有这些CSV文件结合在一起,以便我们可以看到每天的趋势。

因此,如果我们想查看这个数据集的前几行,我们可以再次使用% sh,然后继续使用它。% sh只允许我们运行bash命令。bash命令是,如果你曾经在终端窗口中工作过,这些都是你在终端窗口中运行的命令。但这只是让我们能够真正细粒度地访问这个集群中正在发生的事情。如果我们用head键看一下前几行,我们可以看到这里有很多不同的列。我们有这个FIPS列,我们有admin 2,省,州,country_region,等等。我们等下会讲到这些的具体含义。

这些都是所谓的bash命令。这实际上是在运行Python代码。这里我们要导入熊猫。就像我说的,pandas是一个开放源码的库,供大家使用。bob下载地址所以这是一堆别人写的代码你可以用。这就是所谓的别名。我们会导入熊猫库。然后当我们用这个作为pd时,这是你会在数据科学代码中反复看到的。这就是我们接触熊猫的惯例。现在我们可以称它为pd。reedcsv and we can use the reed CSV functionality from pandas. So let’s go ahead and execute this command. And just for reference, by pressing Control + Enter or Command + Enter, depending on what type of system you’re using, that’s how you can execute these cells. You can also click over here and click run cell here too if you prefer to do that.

所以我打电话给pd。reedcsv和我传递的这个长路径,使我能够访问4/11-2020年的每日报告。这里我们可以看看这个文件里到底有什么。首先我们有这个下标,这是文件中给定行的下标。然后我们有这个FIPS编号。这是区分不同区域的特殊约定。所以这是,我相信这是美国联邦公约。这对我们来说并不是那么重要。admin 2列,如果区域在美国境内,这将是美国境内的县。所以我们会看到一些没有值的其他国家没有admin值。 And province_state, if it’s within the US, this is gonna be the state within the US. If it’s not within the US, it’s going to be the province or region. And we’ll take a closer look at that in a moment. We have the country or region, which is what you would expect. The last update, we’re not really gonna be using that, but that’s when this data was updated. We have latitude, we have longitude, and then we have the actual core data that we’re going to be working with. So we have confirmed, which is the number of confirmed cases. We have deaths, which is the number of the number of people recovered, be somehow careful with this column because not all countries and areas are consistently reporting recoveries from COVID. And then we have active cases as well. And finally we have this combined key. And what the combined key does is just combine a number of these different values. So it combines admin 2 with province_state, with country_region.

很好,我来滚动一下,你可以看到我们有12列。在这个文件中,我们有将近3000行。

之前,你看到我们只是在给定的单元格中执行一行代码,我们可以很容易地组合多行代码。所以我要重新运行这个。它会运行pandas的导入然后它会将这个CSV文件另存为df。这给了我们一个对象,我们可以继续玩这个。我们不需要总是重新导入这个数据集。现在我们有了这个df变量我们有了这个数据集保存在内存中,所以我们可以很容易地访问它并对它执行代码。

很好,首先我们来谈谈数据的总结。这些是一些基本的熊猫操作你们会经常用到。因此,无论你是数据分析师,数据科学家,无论情况如何,我注意到很多人都取得了相当大的成功,他们在与新数据集交互时有一个基本的概述。所以通常情况下,当你看到一个新的数据集时,你都会计算相同的汇总统计数据。所以有一个好的模板来做我们所谓的探索性数据分析是很有帮助的。因此,如果你是一名数据科学家,你试图构建某种数据科学解决方案,首先你要做探索性数据分析,只是为了弄清楚你的数据集中发生了什么。然后这个过程变得更加稳健。最后你把机器学习应用到这个过程中。然后一些魔法确保你的生意自然而然地更成功。差不多就是这样,不过好吧。 So let’s talk a little bit about tab completion. So if I uncomment this code, so this is a comment in Python. Basically, it just indicates the Python that you’re going to ignore whatever comes after it. If I uncomment that code, and I call df., and then hit Tab, I should see the different methods that are available to me. So recall that this is a pandas DataFrame. We use pd.csv in order to import that DataFrame. And when I call .tab, as long as there are resources available on my cluster, I’ll be able to see the different methods that I have associated with it. So for instance, I can call df.abs. And so that’s gonna give me the absolute value in a code. Df.active, not sure what that is, but might be helpful in some case. I can call df.admin2, and that’s probably gonna give me the admin 2 column. But you can see that there’re a number of different methods available here. So this is all code that’s been written for me, and then I can access by using pandas.

我们来看看它是如何工作的。但首先,让我们看看这个df.admin。如果我遇到问题,需要查看实际的文档来了解发生了什么,我可以使用这个帮助命令。如果我在df上求助。Head,它会告诉我它是做什么的。这里,我可以看到我有这个DataFrame类我们有很多不同的参数与它相关联。这也给了我一些如何使用它的例子。

它会告诉我与之相关的不同方法并给出一些细节。所以在这里,我们可以看到这里有相当多的信息,这是因为熊猫是一个非常强大的图书馆。它有很多可用的功能。很好,如果我把这个叫做a df。在没有帮助的情况下,在这里我可以了解数据集的前几行。这将给我数据集的前五行。这样我就不用一遍又一遍地打印整张图了。这里我可以看到前五行,看起来这些都是美国境内的地区。然后如果我想要它的倒数,我可以调用tail。所以df。tail is going to give me the last few lines of it. And so by default, df.tail will give me five lines, just like df.head will, but if I wanna shorten that or lengthen that, I can add a parameter here. And so these arguments rather are how I start to customize my code. And so here I can pass a value, and then in the case of head or in the case of tail, this indicates how many rows I actually wanna be able to see.

如果我想知道数据集总共有多少条记录,我还没有统计数据集,我可以叫它df。shape。这样就能得到数据集的整体形状。所以这里的第一个值是行数。所以我有2966行。第二个值是数据集的宽度。这就是我的列数。因此,在数据科学和数据分析中,通常将其称为特征而不是列,因为特征突出了这样一个事实,即您将在未来将其放入某种模型中。

现在我有很多不同的方法来总结我的数据。值得一提的是,如果我使用command/,我可以很容易地注释不常见的代码。然后在Windows机器上,这可能是Control/但我用了一点来确认。如果我叫df。求和,这将给我整个数据集的列和。对于FIPS,这是这个数字,这是地区的惯例。你可以看到这一列的总和我得到了一个天文数字。这是8.3乘以10 / 7。这对于country_region来说是个很大的数。它把所有的USUSUSs加在一起,这不是特别有用。 Last update time, it’s adding together all those strings. So that’s not super helpful. Lat and long, that’s not very helpful either. But these numbers are helpful. So now it gives me the sum of the confirmed cases, the numbers of deaths, the number of recovered, and the number of active. And so this is quite helpful. So this gives me a sense that, at this point in time, in their mind that this was on the 11th, so this was maybe four or five days ago, the number of confirmed cases it looks like it was, what is that, 1.77 million cases. And I know today it’s a little bit higher than that, actually quite a bit higher than that. But this gives me a sense for generally speaking, what are some of the summary statistics that allow me to get a sense for what’s going on within this dataset?

如果我想求最小值,我也可以这样做。我可以看到FIPS的最低值是66,我可以看到最低的经纬度,如果这对我有帮助的话。确诊病例数量最低,零死亡,零康复,零活跃,零。因此,在这个数据集中,你可以看到这里的一些国家或地区确实没有确诊病例。值得一提的是,这是列最小值。因此,有可能一个国家的确诊病例为零,而另一个国家的死亡病例为零。这个国家不一定是阿富汗。然而,阿富汗是从字符串的角度来看的,因为这是一个字符串值。这是最小值。

我也可以看一下最大值。所以在这里,我可以看到确诊病例的最大值是16.3万例,我们将在一秒钟内更详细地讨论这一点。如果我只做一个计数,这将给我一个在我的数据集中所有非空值的计数。现在我可以看到这些计数看起来就像,或者说这些反映了我的数据集中的行数。

我也可以看平均值。平均值会告诉我,在我的数据集中,平均有多少人死亡,多少人康复,多少人活跃。现在值得记住的是,如果在美国,这些都是在县一级统计的。所以有些信息可能看起来有点错误,因为它不是在国家层面上完成的。我们马上就会处理这个问题。最后来看标准差。标准差能让我了解数据的分布情况。那么这些数据在均值上的分布是多少呢?

很好,这是一种计算基于列的状态或基于列的值的方法。如果我调用这个df。describe,它会为我们做很多这样的操作。这里我可以看到,这些不同的数值。所以describe函数会忽略非数值。通过这些不同的数值,我可以看到计数器值,平均值,标准差,最小值,最大值。这些是不同的四分位数。第50百分位,这是中位数第25百分位和第75百分位。这给了我一个数据。那么它分散了多少(听不清)大部分值是在平均值附近还是我的很多数据离平均值更远?这就是百分位标准差的含义。 So I can get a sense for what the average number of case is. So the average number of cases is gonna be around 598. And so bear in mind, this is gonna be county level data within the US, so that’s gonna off these numbers a little bit and we’ll handle that in a second. Cool, so now let’s go on to slicing and munging data. And so this will give us a sense for how to do some more robust manipulations around it.

如果我只是想找出确认的情况,我可以用这个语法。这是特定于pandas的语法。如果您以前使用过Python,您可能会在Python中认识到这些方括号用于列表,但一般来说,在其他地方不经常使用这种语法。这是熊猫特有的,但你用得越多,它就会变得更自然。如果我叫df(已确认)我可以得到这一列的意义。这里我可以看到df得到的各个值。然后如果我想,如果我想缩短这个,我也可以调用。head。所以有一件事对了解熊猫很有帮助,那就是使用所谓的方法链接是很常见的。这里我索引,我抓取这一列然后添加一个方法。所以我们通常会把不同的方法串在一起。 And so here I can start to use these individual components and make a slightly more complex analysis based upon it. And so you will see the syntax quite a bit. And you’ll see as we go on in this notebook, this will become more and more complex.

嗯,很酷。如果我想看一下这个数字或者我有的列,我可以这样看。如果我想索引多个列,我可以这样做。这里我用的是同样的方括号,但我传入的是一个列表。这里,我有双方括号这表示我想在这里多列。

如果我想增加一列,我可以这样做。我可以记作df。date, date在这里不存在,但是我可以通过这一列。我将使用这个date time对象。我不会讲太多细节,但只要知道这允许我有一个特定的格式,特定于一个日期。然后如果我看一下它的前几个值,它看起来像这样。这里我可以从旧列中创建新列。我可以说取两列并将它们相加,或者我可以创建一个新列并让它们都是相同的值。在这种情况下,是白天。

以上介绍了如何在列级上建立索引。如果我想在行级上索引,我可以这样做。我可以记作df。loc, L-O-C,这就给出了位置。这里,我传递的第一个值是我想要的特定行。在第二个值中,这应该看起来像我想要的列。所以这个的值和这个差不多。这里,它说我想从第0个值开始。所以Python是零索引。这意味着,如果你要索引某个东西,你会从0开始,而不是从1开始。 So I tell it that I wanna go from the zero to the 10th value. And so that’s how I pull out those values here. And so this is gonna be the same thing as this first line. And so this gives me a sense of a how to slice on a horizontal and a vertical or from horizontal and vertical perspective. And if I just wanna pull out the first column and the first row, I can do something like this. And so that’ll give me the first FIPS number that appears within the dataset.

好的,我讲了一下这个数据集中的区域是如何不同的。如果我想知道它到底是什么样子的,我可以把它叫做。value_counts。在country_region中,我们在这一列,我将它命名为。value_counts它会告诉我这个国家有多少个区域。所以在美国,你可以看到我有大约2700个,那是因为我们有美国的县级信息。在美国,我们有大约2700个县在这里。在中国,我认为这些是省份,加拿大也是,英国,法国等等。所以你可以看到,对于这些国家中的少数几个,我有比其他国家更详细的数据。至于突尼斯、智利、阿联酋、斐济等国家,我这里只有国家层面的数据。

FIPS是什么?我们讨论过它是什么,但我们并不关心。FIPS不是我们一定要用的东西。如果要去掉这一列,我们可以用df。drop,然后我们可以调用FIPS,我们可以给它一个访问。这个访问只是表示我们要删除列而不是删除行。它会寻找一行,我们指定access = 1。我们可以这样运行它。然后如果我们叫df。列,我们应该看到这些列都和以前一样,但是这里不再有FIPS编号。 And so bear in mind that here, when I call it df.drop, I re save this to df. So I overwrote the original DataFrame with a new DataFrame, which is df but without the FIPS column.

很酷,现在我们来看看确诊病例。如果我想对DataFrame排序,我可以调用df。sort_values然后我可以传入我要排序的列。在这种情况下,我将按已确认的情况排序我将设置升序为false。如果我设这个等于真,记住,真或假总是大写的,如果我设这个等于真,你可以看到我先有了所有的零情况。但是相反,我想让这个等于false。所以现在你可以看到西班牙确诊病例最多,意大利第二,然后是法国,然后是德国,然后是美国。这看起来有点奇怪。这看起来很奇怪的原因是,我们在美国有国家级别的粒度。这是特别在纽约地区。 And so here you can see as 4/11, we had about 98,000 confirmed cases within New York City.

如果我们只想看看美国国内发生了什么,我们可以这样称呼它。因此我们将其命名为df[Country_region] == US。

如果我运行这个,它只会给我DataFrame其中country_region等于US。

这里我总是可以调用。head让它更短一些在一天结束的时候要少滚动一点。

好,这就是我如何根据一个值筛选一个列。如果我想根据多个不同的值筛选一个列,会发生什么?如果我想这样做,我可以写df[Country_region] ==US,我要把这些都放在括号里然后加上这个10%然后加上province_state等于加州admin 2等于旧金山。这就是我现在的基地。我住在旧金山县,这个县的名字和城市的名字是一样的。所以在4月11日,你可以看到有857例确诊病例,13例死亡,而恢复的数字,我不太相信,因为我不相信所有这些不同的地区都以相同的方式报告恢复数字。但总的来说,我相信确认的数字和死亡人数。

很好,这让我了解了如何根据多个值进行筛选。你可以玩这个。你可以改变它来匹配你所在的区域。

如果我想回答不同的问题呢?例如,哪个国家的确诊病例最多?在这种情况下,我希望能够根据国家对我的数据进行分组然后对它进行某种操作。我可以记作df。我可以按国家或地区分组。这个返回的是这个DataFrame组对象。所以我们真正需要知道的是这是一个特定类型的对象,它返回给我们,但它确实需要一些其他额外的方法。这里我用df。group在上面,我将有相同的country_region,然后我将取出确诊病例。我要对它调用。sum,然后对值进行排序。 And so this is just that chaining that I talked about before. And so sometimes this can be a little bit challenging to read, but when you’re writing this code, normally you start by something like this. So you might start by saying df.groupby, and then you wanna do confirmed, so it just gives you the confirmed values. And then maybe you do this sum on top of this, and then you see that I get sum across all of these different values and then you would change sort values on top of that. And so when you’re coding, you’re normally doing this in an iterative process. It’s not like you’re writing all of this code at once. And so when you see these longer chains of methods, never be intimidated by it. The person who wrote this originally was writing one method at a time, usually executing it, making sure it looks like they were expecting it, and then going on to the next one.

酷,现在我按照国家或地区进行了分组,查看了确诊病例并进行了汇总,看起来美国确诊病例最多,第二是西班牙,第三是意大利,还有法国、德国和中国。所以,首先是美国,然后是一些欧洲国家,然后是中国。

如果我要问这个问题,美国哪个州的病例最多?我可以把我们之前做过的联系起来。这里我要按国家或地区过滤等于US。我要按省和州分组。

我要取出确认的值,做求和,然后对值排序。

这里,这给了我来自美国的州级信息。所以我看到纽约的确诊病例最多,其次是新泽西州、密歇根州、马萨诸塞州等。

很好,现在我想继续讲一些丢失数据,重复值之类的东西。这就是做任何类型的数据分析工作或数据科学工作的现实,你总是要处理异常值,没有值,诸如此类的事情。所以能够以一种非常有效的方式来做这件事可以让你避免花太多时间做这件事。所以我总可以记为df。Isnull,当值为null时返回true,否则返回false。我读这个的方法是,看这里的admin 2列在尾部至少有四个不同的no值。然后是country_region,看起来这些都是空没有值。所以如果我对它调用求和,它会给我一列没有值的数。很多都是缺少admin值的。这是因为我们没有很多数据的县级信息。 And you’ll see some of the other missing values as well, including lat long for 59 different records I have.

现在我们来看看有多少个不同的国家。这里我可以调用df[country_region]我可以调用。unique,然后调用。shape。然后我可以看到这里有185个不同的独特的国家。如果我想做同样的事情,我也可以调用drop duplicate,这也会给我唯一的值。

最后,我总是可以用df。fillna,然后我可以传入一些值来填充。这里我称它为df。fillna和我没有传入可用的数据。现在你可以看到哪里没有值了。突然,我有了绳子。如果你想做所谓的输入,这可能会很有帮助,在这里你不填任何值,用某种标准值。很好,我最后想讲的是可视化。所以在像Python或R这样的语言中,你有非常非常健壮的可视化库。我们这里要用到的是core pandas中可用的,但还有很多其他的库可供你使用。 And so a lot of pythonists start with matplotlib. This is the main plotting library within Python. There’re also at our library such as Seaborn and Pyplot and a number of other ones. And so these are largely open source libraries and so you can use these in quite the same way. But I’m just gonna run this, import for mapplotlib and then call this % mapplotlib in line. All of this does is indicate to Databricks that we wanna render these images for me. And so we wanna make sure we show whatever’s there. So I’m gonna be looking at just the US-based data. So I’m gonna go ahead and create a subset of my original DataFrame. I’m gonna call it us_subset_df and then I’m just gonna pass in this code that you saw before, df[country_region] == US. Cool, so now I have this subset. So now we can start to ask different questions about, say, the number of distribution of deaths by US states and territories. And so oftentimes when we talk about distributions, we’re talking about for deaths. What generally speaking is the number of different values that we have for different states. And so here we can see that we’re, sorry, let me take a step back. So the way that this is operating is we’re going to bend together the number of different debts that we have. So say all of the deaths between zero and 50, we’re gonna put together in the same bin and we’re gonna visualize it this way. And so that way we can get a sense for are there any outliers within my dataset? And so if we take a look at this one, here we can see that there’s one outlier out here, but normally we have much lower number of deaths. But if we want a little bit more granularity, we can change this bins parameter within this histogram. And so here I’m just gonna go through, do a group by, sum of the deaths. So this will give me deaths by state. And then I’m going to to call .hist in order to get a histogram. And then PATS in this bins parameter. And so if I execute this, you can see that I have a histogram that has a little bit more granularity. So you can see that I have one outlier way out here. This is likely gonna be New York. And then you can see the majority of the states have much lower death accounts.

如果我想知道确诊病例与死亡之间的关系,我可以使用。plot这个数据框架,然后调用。scatter。然后我只需要定义x轴和y轴上的坐标。这让我可以想象确诊和死亡。你可以看到这两者之间似乎有某种相关性然后我在这里有一个离群值。如果我想移除这个异常值,我可以调用us_subset_tf。我可以计算任何低于1000的死亡人数。然后我可以调用相同的代码。这样我就可以得到相同的值,但没有异常值。所以在这里你可以再次看到确诊病例和死亡之间的相关性。

这段代码我就不讲了,因为我知道时间有点紧,但这能让我们把所有不同的CSV文件合并在一起。我将逐一讲解。如果你上周和我们在一起,这个(听不清)应该看起来有点眼熟。但是你读入了文件名,你改变了一些列。原因是并不是所有的csv都排列完美。这是一些代码来整理一下。我把日期加进去,然后把所有这些组合在一起。所以基本上我把所有这些不同的数据帧组合在这个列表中,然后我可以在最后把它们串联起来。这就给了我所有这些天的数据。它看起来就像这样。 I dropped out some columns as well because of the issue with columns not matching up. But this will give us the core data that we’re looking for. So now we can start to ask questions about how the disease spread over time. So if we group this information by date and look at confirmed cases, so we want the sum of confirmed cases and then we wanna call .plot on this. And so this gives us a sense for the increase of cases over time. So the data goes back to January, and you can see this increase. And so I added this title here. The title is gonna appear at the top of the plot and then I added this rotation as well. So this rotation is for the X-axis. If I didn’t add this rotation, all of these numbers for dates would all overlap with one another. So I added a 45 degree rotation just so I could see this a little bit better.

现在如果我想按病例类型分类,我可以按日期分组,我可以调出确认的死亡率和康复率。我可以把它叫做。sum,这样我就得到了这些不同值的和。然后我可以把它叫做。plot。所以我给它一个标题,我给它同样的循环,我们可以了解确诊病例的数量,死亡人数,以及康复人数。

如果我想看看我们国家的经济增长情况,我也可以这样看。这看起来可能有点复杂,但基本上我把所有这些都括在括号里这样我就可以把这段代码分成几行。然后按country_region, province_state, admin 2进行子集。这和我们之前在旧金山看到的代码是一样的。我按日期分组,找出确认的死亡人数,康复人数,称其为总和,最后称其为plot。这给了我旧金山的具体结果。

所以你可以看到确诊病例的数量呈线性增长。我们发现,相对而言,我们的死亡人数和康复人数都很低。很好,最后这个函数,它只是包装了,我们在这里看到的这行代码,它把所有这些都包装成一个函数。如果你上周上过我们的课,你应该很熟悉你可以(听不清)这段代码用(听不清)来执行函数这样你就可以给这个函数添加你自己的参数。现在我可以画出我的国家,然后我可以看到美国,纽约,纽约市,但我也可以为我特定的州或地区添加不同的参数,如果我想的话。你可以玩这个你可以用这个为你自己的区域自定义结果。所以我知道这有点像熊猫。这样你就能拿到这本笔记本了。你也可以使用实验室。实验室会给你们一些初始代码,让你们做一些绘图练习,这样你们就能了解如何将你们所在区域的情况可视化。 So I want to make sure we had a few minutes at least for some questions. And so why don’t we go ahead and switch to that? So if you have any questions, feel free to include in the Q&A. And then I also ask the TAs to include some questions that came up with some frequency so that we could all address them together. – [Karen] Yeah, Conor, great job. There’re quite a few folks who are asking about pandas versus Koalas and also pandas versus Spark, if you wanna talk about the differences between single node and distributed computing. – Perfect, that sounds good. So the library we used today is pandas. Pandas is what we call a single node technology. And so when we run this DataFrame, when we run any pandas code, it’s always only working on one single machine. And so for those of you who are familiar with Spark, so Spark is a distributed computing system. And so if you have more data that can fit on any one machine, then you’re going to wanna use something like Spark rather than pandas because pandas works really well. Data scientists use it all the time, but it cannot scale beyond one single machine. And so when you use something like Spark, it allows you to distribute that. And so Spark also uses a DataFrame, but that DataFrame is actually, you have a little bit of your data sitting on different nodes across your cluster. And so Brooke mentioned Koalas as well. Koalas is a way of writing Python style code but against a Spark DataFrame rather than a pandas DataFrame. So it’s going to allow you to use this same syntax, but your code is gonna be able to scale across a cluster of machines. And so this will be, if you’re working on any sort of big data problem. And so if you have more data that can fit on any one machine and you don’t know how to work with it, then you’re gonna wanna look into technologies like Spark and Koalas.

- [Karen]很好,然后还有几个关于如何向DataFrame添加记录的问题?

-这是个好问题。我们学习了如何向DataFrame中添加列。还有很多不同的工具可供你使用。如果我们看一看熊猫的小抄,我们应该有一些如何做到这一点的指示。有很多不同的方法可以组合数据。你可以看看这些不同的重塑功能,但你可能想看看这个pd。concat。这将把两个不同的dataframe连接在一起。你可以这样做,你可以水平堆叠,也可以垂直堆叠。但这些类型的值是你要研究的。- [Karen]然后作为之前关于PySpark和Panda的问题的后续,有人问,当你使用一个记录多于另一个记录时,是否有特定的阈值,比如1万条记录,10万条记录,你考虑了一些什么因素? – Yeah, so the main thing you have to consider is how much memory you have available to you. So if you have eight gigabytes worth of memory on one machine, and the data that you have is eight gigabytes, you’re probably going to need to use some sort of distributed technology Spark or whatever the case may be. And so that’s one thing to consider. It’s more than size in memory rather than the overall number of records that you have. And bear in mind that if you have eight gigabytes of memory available to you and eight gigabytes worth of data, that’s likely not going to work because you still need a little bit of extra memory available to you to do any sort of operation. And so I would start to think about using something like Spark, once you’re on the order of maybe tens of millions of rows, but like I said,