熊猫数据集_在日常生活中使用熊猫数据框

熊猫数据集

Before getting started let us do a quick revision, Pandas is a python library that gives elite, and simple to-utilize information structure for data analysis tools for python programming language. Show below is the article which will help you gain your understanding level in the Pandas library. Don’t forget to have a look at it. In fact, this is the official docs. It is worth reading it.

乙 EFORE起步,让我们做一个快速的修改,熊猫是一个Python库 ,让精英,而简单的利用为Python编程语言的数据分析工具的信息结构。 下面显示的文章将帮助您提高对Pandas库的理解水平。 别忘了看一下。 实际上,这是官方文档。 值得一读。

入门所需的必要设置 (Necessary set-up to get you started)

There are two important necessary steps that you need to take care to perform smooth functioning:

您需要注意两个重要的必要步骤才能执行平稳的功能:

  • IDE or Jupyter Notebook: It doesn’t really matter whether you use an IDE like PyCharm, Visual Studio Code, IntelliJ IDEA, or Jupyter Notebook environment. Personally, I use Google Colab with is a Jypter Notebook environment because of its good documentation feature. We can explain things in a clear and more precise manner.

    IDE或Jupyter Notebook :是否使用诸如PyCharmVisual Studio CodeIntelliJ IDEAJupyter Notebook之类的IDE并不重要 环境。 就个人而言,我将Google Colab用于Jypter Notebook环境,因为它具有良好的文档功能。 我们可以用更清晰,更精确的方式解释事情。

  • Python: If this is your first time installing Python, don’t worry I got your back. Follow the article given underneath and follow the means to effectively install Python.

    Python :如果这是您第一次安装Python,请不要担心我会回来的。 按照下面给出的文章并按照有效安装Python的方法进行操作。

Note → If you are using Google Colab or any Jupyter notebook environment, then you can skip the Python installation step.

注意→如果您使用的是 Google Colab 或任何 Jupyter笔记本 环境,则可以跳过Python安装步骤。

情境 (Scenario)

Let’s understand a scenario here and come up with a solution using Pandas DataFrame.

让我们在这里了解一个场景,并提出使用Pandas DataFrame的解决方案。

“Everybody does and loves to do shopping (grocery). Not all the time you grocery shop at the same store, you shop based on discounts and offers (I do). Now obviously it’s a good practice to keep track of shopping details like the store name, location, amount, date, etc.”

“每个人都喜欢购物(杂货店)。 并非所有时候您都在同一家杂货店购物,而是根据折扣和优惠购物(我愿意)。 现在显然,跟踪商店的详细信息(例如商店名称,位置,金额,日期等)是一个好习惯。”

To store and manipulate date I know you will use “Microsoft Excel” but the catch point here is we will use “Pandas DataFrame” which is by far easier and fun to use. Don’t worry, I’ll show you how.

要存储和操作日期,我知道您将使用“ Microsoft Excel ”,但是这里要注意的是我们将使用“ Pandas DataFrame ”,它使用起来更加简单有趣。 不用担心,我会告诉你如何。

Don’t get me wrong, “Microsoft Excel” is where it’s at. But to all those Python enthusiasts this is the best example to understand and take Pandas to next level.

不要误会我的意思,“ Microsoft Excel ”就在这里。 但是,对于所有这些Python爱好者来说,这是理解和将Pandas提升到更高水平的最佳示例。

(Solution)

Let’s solve the above scenario using Pandas DataFrame. Now in order to that, you need to follow certain steps to not commit mistakes. I will provide a systematic way of using Pandas so that you can use it mechanically in your upcoming projects or whatever.

让我们使用Pandas DataFrame解决上述情况。 现在,为此,您需要按照某些步骤操作,以免犯错。 我将提供一种使用Pandas的系统方法,以便您可以在即将进行的项目中或任何其他方面机械地使用它。

导入Pandas DataFrame库 (Importing the Pandas DataFrame library)

Let’s use the concept of aliasing and use the pandas as pd. So in the later steps, rather than using pandas every time, we can just tell pd.

让我们使用别名的概念,将pandas用作pd 。 因此,在后续步骤中,不要使用pandas 每次,我们只能告诉pd

import pandas as pd

Thanks to whoever introduced the concept of aliasing.

感谢谁介绍了别名的概念。

首次将购物详细信息存储在列表中 (Storing the shopping details in a list — for the first time)

For those of you who don’t about Python list. Please look it up here. Since I’m currently living in Regina, Saskatchewan, Canada. So most of the grocery stores would be similar or different when compared to yours. Please bear that in mind. It really does not matter, you can feed the data as per your choice.

对于那些不了解Python的人。 请在这里查找。 由于我目前居住在加拿大萨斯喀彻温省里贾纳 。 因此,与您的杂货店相比,大多数杂货店将是相似或不同的。 请记住这一点。 没关系,您可以根据自己的选择输入数据。

date = ['1-9-20', '3-9-20', '3-9-20', '6-9-20', '9-9-20']storeName = ['Walmart', 'Real Canadian Superstore', 'Co-op Food Store', 'Sobeys', 'M&M Food Market']storeLocation = ['Gordon Road', 'Albert Street', 'Albert Street',       'Quance Street', 'Gordon Street']amount = [55.65, 21.62, 7.10, 15.56, 5.85]

Now, what do I mean by “for the first time”, is that later say suppose if you keep on doing grocery shopping in the future, rather than storing the values manually in the list. The list would be grown as long as a train. Also, this is not recommended. So I have written a way to handle this situation in the lower sections.

现在,“ 第一次 ”的意思是,稍后再说,假设您以后是否继续进行杂货店购物,而不是手动将值存储在列表中。 这个清单将和火车一样长 。 另外,不建议这样做。 因此,我在下面的部分中编写了一种处理这种情况的方法。

创建一个熊猫数据框来存储所有列表值 (Creating a Pandas DataFrame to store all the list values)

Here, since we have all the values store in a list, let’s put them in a DataFrame. We can use pd.DataFrame() and pass the value, which is all the list in this case.

在这里,由于所有值都存储在列表中,因此将它们放在DataFrame中。 我们可以使用pd.DataFrame()并传递值,这是本例中的所有列表。

df = pd.DataFrame({'Date': date, 
'Store Name': storeName,
'Store Location': storeLocation,
'Amount Purchased': amount})df

On executing this you will get a beautiful DataFrame as seen below

执行此操作后,您将获得一个漂亮的DataFrame,如下所示

Image for post
Photo by Tanu Nanda Prabhu Tanu Nanda Prabhu摄

将购物详细信息作为用户的输入,以供将来访问 (Taking the shopping details as input from the user — for future visits)

As I said, it’s not a very good practice to store all the values in a list. In this way, we automatically make the list grow. There is a neat and cute way to handle this kind of situation. First, let’s take the input from the input and store them in temporary variables as shown below:

如我所说,将所有值存储在列表中并不是一个很好的做法。 这样,我们将自动使列表增长。 有一种巧妙而可爱的方式来处理这种情况。 首先,让我们从输入中获取输入并将其存储在临时变量中,如下所示:

dateNew = input("Enter the date in dd-mm-yy format ---> ")storeNameNew = input("Enter the name of the store ---> ")storeLocationNew = input("Enter the location of the store ---> ")amountNew = float(input("Enter the total amount purchased ---> "))

So on the next day, I mean 10–9–20 I went to the grocery store to shop for some spices with Gordon Ramsay, just kidding I went alone. Below are the details of the new store.

所以第二天,我的意思是我10–9–20我去杂货店和Gordon Ramsay一起买了些香料,开玩笑的是我一个人去。 以下是新商店的详细信息。

Enter the date in dd-mm-yy format ---> 10-9-20 Enter the name of the store ---> India Market Enter the location of the store ---> Albert Street Enter the total amount purchased ---> 24.68

将新数据追加到现有列表 (Appending the new data to the existing list)

This is an obvious step because we need to append the new data (shopping details) to the existing DataFrame. We can do this with the help of append() Python.

这是显而易见的步骤,因为我们需要将新数据(购物详细信息)附加到现有的DataFrame中。 我们可以借助append() Python做到这一点。

date.append(dateNew)
storeName.append(storeNameNew)
storeLocation.append(storeLocationNew)
amount.append(amountNew)

通过DataFrame显示更新的结果 (Displaying the updated result via a DataFrame)

This step is trivial because all we are doing is just displaying all the updated results from the above step as a DataFrame. Similar to the “Creating a Pandas DataFrame to store all the list values” step shown above.

这一步很简单,因为我们所做的只是将上述步骤中所有更新的结果显示为DataFrame。 类似于上面显示的“ 创建熊猫数据框以存储所有列表值 ”步骤。

df = pd.DataFrame({'Date': date, 
'Store Name': storeName,
'Store Location': storeLocation,
'Amount': amount})df

After executing this piece of code, you will be prompted with the updated result of the new grocery store added in the previous step

执行完这段代码后,将提示您上一步中添加的新杂货店的更新结果

Image for post
Photo by Tanu Nanda Prabhu 塔努·南达·普拉布(Tanu Nanda Prabhu)摄

There you go, you have successfully handled the scenario like a piece of cake. You can keep on adding more data (shopping details) and maintain monthly or yearly wise.

到此为止,您已经像小菜一碟般成功地处理了该方案。 您可以继续添加更多数据(购物明细),并按月或按年维护。

奖金 (Bonus)

您可以在现有DataFrame上执行的漂亮技巧 (Nifty tricks that you can perform on the existing DataFrame)

Shown below are some tips and tricks that you can perform or do in your free time. Because they are that easy to understand. Let me get into it.

下面显示的是您可以在空闲时间执行或执行的一些提示和技巧。 因为它们很容易理解。 让我进入它。

绘制条形图 (Plotting a bar plot)

It’s always a good practice to plot the data for better readability. Now for plotting let us use the column Amount so that we know how much we have spent till now with the help of a plot. This can be done by using df.plot.bar() as shown below

绘制数据以提高可读性始终是一个好习惯。 现在进行绘图,让我们使用“ Amount ”列 这样一来,我们就可以知道在情节的帮助下我们花了多少钱。 可以使用df.plot.bar()如下所示

Note: Always remember only numeric data can be plotted in Pandas

注意:请记住,只能在熊猫中绘制数字数据

df.plot.bar()

On writing this one line of code you can see a beautiful plot as shown below:

在编写这一行代码时,您可以看到一个漂亮的图,如下所示:

Image for post
Photo by Tanu Nanda Prabhu 塔努·南达·普拉布(Tanu Nanda Prabhu)摄

删除整行/列 (Deleting an entire row/column)

Say suppose we need to delete the last row because there is a mistake in the entry so this can be performed as follows. Now to delete an entire row we need to use drop() in Pandas as shown below:

假设我们需要删除最后一行,因为条目中有错误,因此可以按以下步骤执行。 现在要删除整行,我们需要在Pandas中使用drop() ,如下所示:

df = df.drop([5])
df

Here the value 5 is the index value of the last row. By executing this we get

这里的值5 是最后一行的索引值。 通过执行此操作,我们得到

Image for post
Photo by Tanu Nanda Prabhu 塔努·南达·普拉布(Tanu Nanda Prabhu)摄

Deleting the column is like that of a row all you have to do is pass the column names to delete in this case like df.drop([column_name, axis = 1]) the axis = 1 is important here. Say suppose you need to delete the column Amount in the DataFrame then all you need to say is:

删除列就像一行的一样,您要做的就是在这种情况下传递要删除的列名称,例如df.drop([column_name, axis = 1]) axis = 1在这里很重要。 说假设你需要删除列Amount的数据帧,那么所有你需要说的是:

df = df.drop(['Amount'], axis = 1)
df

By executing this you can now see that the column Amount has now been dropped.

通过执行此操作,您现在可以看到列Amount 现在已删除。

Image for post
Photo by Tanu Nanda Prabhu 塔努·南达·普拉布(Tanu Nanda Prabhu)摄

修改特定条目 (Modifying specific entries)

Suppose we need to update a specific entry in the DataFrame, in this case, the 4th index with the store name M&M Food Market has a mistake in its store Location (wrong entry) Gordon Street, we need to correct it to Gordon Road. To do this just use:

假设我们需要更新DataFrame中的特定条目,在这种情况下,商店名称M&M Food Market的第4个索引的商店位置(输入错误) Gordon Street错误,我们需要将其更正为Gordon Road 。 为此,只需使用:

df['Store Location'][4] = "Gordon Road"
df

We need to know the index of the particular entry to update the entry, so after executing the above we get the updated result

我们需要知道特定条目的索引来更新条目,因此在执行上述操作后,我们将获得更新后的结果

Image for post
Photo by Tanu Nanda Prabhu 塔努·南达·普拉布(Tanu Nanda Prabhu)摄

结论 (Conclusion)

Well, congratulations guys you have successfully completed reading/implementing this beautiful article “Using the Pandas DataFrame in Day-To-Day Life”. Now, this is not the end, there are many other methods or functions of DataFrame that we can use and take it to next level. I have only covered only the basics here. If you guys find something new or creative, then comment it down below. I hope you guys have learned something new today. Stay tuned for more updates, until then see you next time. Bye Have a good day and stay safe!

好了,恭喜您,您已经成功阅读/实现了这篇漂亮的文章“ 在日常生活中使用Pandas DataFrame ”。 现在,这还不是终点,我们可以使用DataFrame的许多其他方法或功能,并将其带入新的高度。 我只在这里介绍了基础知识。 如果您发现新的或有创意的东西,请在下面将其注释掉。 我希望你们今天学到了一些新东西。 请继续关注更多更新,直到下次再见。 再见,祝您有美好的一天并保持安全!

翻译自: https://towardsdatascience.com/using-the-pandas-dataframe-in-day-to-day-life-91859ee12cca

熊猫数据集

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值