⚠️ Note This post is a part of Learning data analysis with python series. If you haven’t read the first post, some of the content won’t make sense. Check it out here.

Note️ 注意 -这篇文章是使用python系列学习数据分析的一部分。 如果您还没有阅读第一篇文章,那么其中一些内容将毫无意义。 在这里查看 。

In the previous article, we talked about Pandas Series, working with real world data and handling missing values in data. Although series are very useful but most real world datasets contain multiple rows and columns and that’s why Dataframes are used much more than series. In this post, we’ll talk about dataframe and some operations that we can do on dataframe objects.

在上一篇文章中,我们讨论了Pandas系列,它使用现实世界的数据并处理数据中的缺失值。 尽管序列非常有用,但是大多数现实世界的数据集都包含多个行和列,这就是为什么使用数据框比序列更多的原因。 在本文中,我们将讨论数据框以及我们可以对数据框对象执行的一些操作。

什么是DataFrame? (What is a DataFrame?)

As we saw in the previous post, A Series is a container of scalars,A DataFrame is a container for Series. It’s a dictionary like data structure for Series. A DataFrame is similar to a two-dimensional hetrogeneous tabular data(SQL table). A DataFrame is created using many different types of data such as dictionary of Series, dictionary of ndarrays/lists, a list of dictionary, etc. We’ll look at some of these methods to create a DataFrame object and then we’ll see some operations that we can apply on a DataFrame object to manipulate the data.

正如我们在上一篇文章中看到的,A Series是标量的容器,DataFrame是Series的容器。 这是一个字典,类似于Series的数据结构。 DataFrame类似于二维异构表格数据(SQL表)。 DataFrame是使用许多不同类型的数据创建的,例如Series字典,ndarrays / lists字典,字典列表等。我们将研究其中的一些方法来创建DataFrame对象,然后再看一些我们可以应用到DataFrame对象上的操作来操纵数据。

使用Series字典的DataFrame (DataFrame using dictionary of Series)

In[1]:    d = {    'col1' : pd.Series([1,2,3], index = ["row1", "row2", "row3"]),    'col2' : pd.Series([4,5,6], index = ["row1", "row2", "row3"])    }    df = pd.DataFrame(d)Out[1]:        col1    col2row1     1       4row2     2       5row3     3       6

As shown in above code, the keys of dict of Series becomes column names of the DataFrame and the index of the Series becomes the row name and all the data gets mapped by the row name i.e.,order of the index in the Series doesn’t matter.

如上面的代码所示,Series的dict键成为DataFrame的列名,Series的索引成为行名,并且所有数据都按行名映射,即Series中索引的顺序不物。

使用ndarrays / list的DataFrame (DataFrame using ndarrays/lists)

In[2]:    d = {        'one' : [1.,2.,3.],        'two' : [4.,5.,6.]    }    df = pd.DataFrame(d)Out[2]:    one  two0   1.0  4.01   2.0  5.02   3.0  6.0

As shown in the above code, when we use ndarrays/lists, if we don’t pass the index then the range(n) becomes the index of the DataFrame.And while using the ndarray to create a DataFrame, the length of these arrays must be same and if we pass an explicit index then the length of this index must also be of same length as the length of the arrays.

如上面的代码所示,当我们使用ndarrays / lists时,如果不传递索引,则range(n)将成为DataFrame的索引。当使用ndarray创建DataFrame时,这些数组的长度必须相同,并且如果我们通过显式索引,则该索引的长度也必须与数组的长度相同。

使用字典列表的DataFrame (DataFrame using list of dictionaries)

In[3]:    d = [        {'one': 1, 'two' : 2, 'three': 3},        {'one': 10, 'two': 20, 'three': 30, 'four': 40}    ]    df = pd.DataFrame(d)Out[3]:    one two three four0    1   2   3     NaN1    10  20  30    40.0In[4]:    df = pd.DataFrame(d, index=["first", "second"])Out[4]:        one two three fourfirst    1   2   3     NaNsecond   10  20  30    40.0

And finally, as described above we can create a DataFrame object using a list of dictionary and we can provide an explicit index in this method,too.

最后,如上所述,我们可以使用字典列表创建DataFrame对象,并且也可以在此方法中提供显式索引。

Although learning to create a DataFrame object using these methods is necessary but in real world, we won’t be using these methods to create a DataFrame but we’ll be using external data files to load data and manipulate that data. So, let’s take a look how to load a csv file and create a DataFrame.In the previous post, we worked with the Nifty50 data to demonstrate how Series works and similarly in this post, we’ll load Nifty50 2018 data, but in this dataset we have data of Open, Close, High and Low value of Nifty50. First let’s see what this dataset looks like and then we’ll load it into a DataFrame.

尽管学习使用这些方法创建DataFrame对象是必要的,但在现实世界中,我们不会使用这些方法来创建DataFrame,而是将使用外部数据文件加载数据并操纵该数据。 因此,让我们看一下如何加载一个csv文件并创建一个DataFrame。在上一篇文章中,我们使用Nifty50数据来演示Series的工作原理,与此类似,在本文中,我们将加载Nifty50 2018数据,但是在本文中在数据集中,我们具有Nifty50的开盘价,收盘价,高价和低价的数据。 首先,让我们看看该数据集的外观,然后将其加载到DataFrame中。

Nifty 50 Data (2018)
Nifty 50数据(2018)
In[5]:    df = pd.read_csv('NIFTY50_2018.csv')Out[5]:        Date      Open        High        Low        Close0   31 Dec 2018  10913.20   10923.55    10853.20    10862.551   28 Dec 2018  10820.95   10893.60    10817.15    10859.902   27 Dec 2018  10817.90   10834.20    10764.45    10779.803   26 Dec 2018  10635.45   10747.50    10534.55    10729.854   24 Dec 2018  10780.90   10782.30    10649.25    10663.50...     ...         ...        ...         ...         ...241 05 Jan 2018  10534.25   10566.10    10520.10    10558.85242 04 Jan 2018  10469.40   10513.00    10441.45    10504.80243 03 Jan 2018  10482.65   10503.60    10429.55    10443.20244 02 Jan 2018  10477.55   10495.20    10404.65    10442.20245 01 Jan 2018  10531.70   10537.85    10423.10    10435.55In[6]:    df = pd.read_csv('NIFTY50_2018.csv', index_col=0)Out[6]:                  Open        High        Low        Close    Date31 Dec 2018     10913.20    10923.55    10853.20    10862.5528 Dec 2018     10820.95    10893.60    10817.15    10859.9027 Dec 2018     10817.90    10834.20    10764.45    10779.8026 Dec 2018     10635.45    10747.50    10534.55    10729.8524 Dec 2018     10780.90    10782.30    10649.25    10663.50     ...           ...        ...         ...          ...05 Jan 2018     10534.25    10566.10    10520.10    10558.8504 Jan 2018     10469.40    10513.00    10441.45    10504.8003 Jan 2018     10482.65    10503.60    10429.55    10443.2002 Jan 2018     10477.55    10495.20    10404.65    10442.2001 Jan 2018     10531.70    10537.85    10423.10    10435.55

As shown above, we have loaded the dataset and created a DataFrame called df and looking at the data, we can see that we can set the index of our DataFrame to the Date column and in the second cell we did that by providing the index_col parameter in the read_csv method.

如上所示,我们已经加载了数据集并创建了一个名为df的DataFrame并查看数据,我们可以看到可以将DataFrame的索引设置为Date列,在第二个单元格中,我们通过提供index_col参数来完成此操作在read_csv方法中。

There are many more parameters available in the read_csv method such as usecols using which we can deliberately ask the pandas to only load provided columns, na_values to provide explicit values that pandas should identify as null values and so on and so forth. Read more about all the parameters in pandas documentation.

read_csv方法中还有许多可用的参数,例如usecols ,我们可以使用这些参数故意要求熊猫仅加载提供的列,使用na_values提供熊猫应将其标识为空值的显式值,依此类推。 在pandas 文档中阅读有关所有参数的更多信息。

Now, let’s look at some of the basic operations that we can perform on the dataframe object in order to learn more about our data.

现在,让我们看一下可以对dataframe对象执行的一些基本操作,以了解有关数据的更多信息。

In[7]:    # Shape(Number of rows and columns) of the DataFrame    df.shapeOut[7]:    (246,4)In[8]:    # List of index    df.indexOut[8]:    Index(['31 Dec 2018', '28 Dec 2018', '27 Dec 2018', '26 Dec 2018',       '24 Dec 2018', '21 Dec 2018', '20 Dec 2018', '19 Dec 2018',       '18 Dec 2018', '17 Dec 2018',       ...       '12 Jan 2018', '11 Jan 2018', '10 Jan 2018', '09 Jan 2018',       '08 Jan 2018', '05 Jan 2018', '04 Jan 2018', '03 Jan 2018',       '02 Jan 2018', '01 Jan 2018'],      dtype='object', name='Date', length=246)In[9]:    # List of columns    df.columnsOut[9]:    Index(['Open', 'High', 'Low', 'Close'], dtype='object')In[10]:    # Check if a DataFrame is empty or not    df.emptyOut[10]:    False

It’s very crucial to know data types of all the columns because sometimes due to corrupt data or missing data, pandas may identify numeric data as ‘object’ data-type which isn’t desired as numeric operations on the ‘object’ type of data is costlier in terms of time than on float64 or int64 i.e numeric datatypes.

了解所有列的数据类型非常关键,因为有时由于损坏的数据或缺少的数据,大熊猫可能会将数字数据标识为“对象”数据类型,这是不希望的,因为对“对象”类型的数据进行数字运算是在时间上比在float64int64上更昂贵,即数值数据类型。

In[11]:    # Datatypes of all the columns    df.dtypesOut[11]:    Open     float64    High     float64    Low      float64    Close    float64    dtype: object

We can use iloc and loc to index and get the particular data from our dataframe.

我们可以使用iloc和loc进行索引并从数据框中获取特定数据。

In[12]:    # Indexing using implicit index    df.iloc[0]Out[12]:    Open     10913.20    High     10923.55    Low      10853.20    Close    10862.55    Name: 31 Dec 2018, dtype: float64In[13]:    # Indexing using explicit index    df.loc["01 Jan 2018"]Out[13]:    Open     10531.70    High     10537.85    Low      10423.10    Close    10435.55    Name: 01 Jan 2018, dtype: float64

We can also use both row and column to index and get specific cell from our dataframe.

我们还可以使用行和列来索引并从数据框中获取特定的单元格。

In[14]:    # Indexing using both the axes(rows and columns)    df.loc["01 Jan 2018", "High"]Out[14]:    10537.85

We can also perform all the math operations on a dataframe object same as we did on series.

我们也可以像处理序列一样对数据框对象执行所有数学运算。

In[15]:    # Basic math operations    df.add(10)Out[15]:                      Open        High        Low        Close    Date31 Dec 2018     10923.20    10933.55    10863.20    10872.5528 Dec 2018     10830.95    10903.60    10827.15    10869.9027 Dec 2018     10827.90    10844.20    10774.45    10789.8026 Dec 2018     10645.45    10757.50    10544.55    10739.8524 Dec 2018     10790.90    10792.30    10659.25    10673.50     ...           ...        ...         ...          ...05 Jan 2018     10544.25    10576.10    10530.10    10568.8504 Jan 2018     10479.40    10523.00    10451.45    10514.8003 Jan 2018     10492.65    10513.60    10439.55    10453.2002 Jan 2018     10487.55    10505.20    10414.65    10452.2001 Jan 2018     10541.70    10547.85    10433.10    10445.55

We can also aggregate the data using the agg method. For instance, we can get the mean and median values from all the columns in our data using this method as show below.

我们还可以使用agg方法汇总数据。 例如,可以使用此方法从数据中所有列获取平均值和中值,如下所示。

In[16]:    # Aggregate one or more operations    df.agg(["mean", "median"])Out[16]:            Open            High            Low            Closemean    10758.260366    10801.753252    10695.351423    10749.392276median  10704.100000    10749.850000    10638.100000    10693.000000

However, pandas provide a more convenient method to get a lot more than just minimum and maximum values across all the columns in our data. And that method is describe. As the name suggests, it describes our dataframe by applying mathematical and statistical operations across all the columns.

但是,熊猫提供了一种更便捷的方法,不仅可以在我们数据的所有列中获得最大值和最小值。 并且describe该方法。 顾名思义,它通过在所有列上应用数学和统计运算来描述我们的数据框。

In[17]:    df.describe()Out[17]:            Open           High            Low             Closecount   246.000000      246.000000      246.000000      246.000000mean    10758.260366    10801.753252    10695.351423    10749.392276std     388.216617      379.159873      387.680138      382.632569min     9968.800000     10027.700000    9951.900000     9998.05000025%     10515.125000    10558.650000    10442.687500    10498.91250050%     10704.100000    10749.850000    10638.100000    10693.00000075%     10943.100000    10988.075000    10878.262500    10950.850000max     11751.800000    11760.200000    11710.500000    11738.500000

And to get the name, data types and number of non-null values in each columns, pandas provide info method.

为了获取每列中的名称,数据类型和非空值的数量,pandas提供了info方法。

In[18]:    df.info()Out[18]:    <class 'pandas.core.frame.DataFrame'>    Index: 246 entries, 31 Dec 2018 to 01 Jan 2018    Data columns (total 4 columns):    #   Column  Non-Null Count  Dtype    ---  ------  --------------  -----    0   Open    246 non-null    float64    1   High    246 non-null    float64    2   Low     246 non-null    float64    3   Close   246 non-null    float64    dtypes: float64(4)    memory usage: 19.6+ KB

We are working with a small data with less than 300 rows and thus, we can work with all the rows but when we have tens or hundreds of thousand rows in our data, it’s very difficult to work with such huge number of data. In statistics, ‘sampling’ is a technique that solves this problem. Sampling means to choose a small amount of data from the whole dataset such that the sampling dataset contains somewhat similar features in terms of diversity as that of the whole dataset. Now, it’s almost impossible to manually select such peculiar rows but as always, pandas comes to our rescue with the sample method.

我们正在处理少于300行的小型数据,因此,我们可以处理所有行,但是当我们的数据中有成千上万的行时,处理如此大量的数据非常困难。 在统计中,“采样”是一种解决此问题的技术。 抽样是指从整个数据集中选择少量数据,以使抽样数据集在多样性方面包含与整个数据集相似的特征。 现在,几乎不可能手动选择这种特殊的行,但是与往常一样,大熊猫通过sample方法来帮助我们。

In[19]:    # Data Sampling -  Get random n examples from the data.    df.sample(5)Out[19]:              Open        High        Low         CloseDate04 Jul 2018 10715.00    10777.15    10677.75    10769.9022 Jun 2018 10742.70    10837.00    10710.45    10821.8514 Mar 2018 10393.05    10420.35    10336.30    10410.9009 Jan 2018 10645.10    10659.15    10603.60    10637.0027 Apr 2018 10651.65    10719.80    10647.55    10692.30

But, executing this method produces different results everytime and that may be unacceptable in some cases. But that can be solved by providing random_state parameter in the sample method to reproduce same result everytime.

但是,每次执行此方法都会产生不同的结果,在某些情况下可能是不可接受的。 但这可以通过在sample方法中提供random_state参数来每次重现相同的结果来解决。

As shown above, we can perform many operations on the DataFrame object to get information of the DataFrame and from the DataFrame. These are just basic operations that we can perform on the DataFrame object, there are many more interesting methods and operations that we can perform on the DataFrame object such as pivot , merge , join and many more. Also, in this given dataset, we have time as the index of our DataFrame i.e this is the TimeSeries dataset and pandas also provide many methods to manipulate the TimeSeries data such as rolling_window.

如上所示,我们可以对DataFrame对象执行许多操作,以获取DataFrame的信息以及从DataFrame获取信息。 这些只是基本的操作,我们可以将数据帧对象执行,还有很多更有趣的方法和操作,我们可以如数据框对象进行pivotmergejoin等等。 同样,在给定的数据集中,我们以时间作为DataFrame的索引,即,这是TimeSeries数据集,而pandas也提供了许多方法来操纵TimeSeries数据,例如rolling_window

That will be all for this post. In the next post we’ll look at some of these methods and we’ll perform 5 analysis tasks using these methods. Till then, you can take a look at the pandas documentation and find more information about DataFrame objects and the methods that can be applied on the DataFrame object.

这就是这篇文章的全部内容。 在下一篇文章中,我们将介绍其中一些方法,并使用这些方法执行5个分析任务。 到那时,您可以看一下pandas文档 ,找到有关DataFrame对象以及可应用于DataFrame对象的方法的更多信息。

Originally Published At : https://www.bytetales.co/pandas-data-frames-learning-data-analysis-with-python/

最初发布于: https : //www.bytetales.co/pandas-data-frames-learning-data-analysis-with-python/

Thank you for reading!

感谢您的阅读!

翻译自: https://medium.com/byte-tales/learning-data-analysis-with-python-pandas-dataframe-2f2d40d6c11f


http://www.taodudu.cc/news/show-995229.html

相关文章:

  • 前端绘制绘制图表_绘制我的文学风景
  • 回归分析检验_回归分析
  • 数据科学与大数据技术的案例_主数据科学案例研究,招聘经理的观点
  • cad2016珊瑚_预测有马的硬珊瑚覆盖率
  • 用python进行营销分析_用python进行covid 19分析
  • 请不要更多的基本情节
  • 机器学习解决什么问题_机器学习帮助解决水危机
  • 网络浏览器如何工作
  • 案例与案例之间的非常规排版
  • 隐私策略_隐私图标
  • figma 安装插件_彩色滤光片Figma插件,用于色盲
  • 设计师的10种范式转变
  • 实验心得_大肠杆菌原核表达实验心得(上篇)
  • googleearthpro打开没有地球_嫦娥五号成功着陆地球!为何嫦娥五号返回时会燃烧,升空却不会?...
  • python实训英文_GitHub - MiracleYoung/You-are-Pythonista: 汇聚【Python应用】【Python实训】【Python技术分享】等等...
  • 工作失职的处理决定_工作失职的处理决定
  • vue图片压缩不失真_图片压缩会失真?快试试这几个无损压缩神器。
  • 更换mysql_Docker搭建MySQL主从复制
  • advanced installer更换程序id_好程序员web前端培训分享kbone高级-事件系统
  • 3d制作中需要注意的问题_浅谈线路板制作时需要注意的问题
  • cnn图像二分类 python_人工智能Keras图像分类器(CNN卷积神经网络的图片识别篇)...
  • crc16的c语言函数 计算ccitt_C语言为何如此重要
  • python 商城api编写_Python实现简单的API接口
  • excel表格行列显示十字定位_WPS表格:Excel表格打印时,如何每页都显示标题行?...
  • oem是代工还是贴牌_代加工和贴牌加工的区别是什么
  • redis将散裂中某个值自增_这些Redis命令你都掌握了没?
  • opa847方波放大电路_电子管放大电路当中阴极电阻的作用和选择
  • 深度学习数据扩张_适用于少量数据的深度学习结构
  • 城市轨道交通运营票务管理论文_城市轨道交通运营管理专业就业前景怎么样?中职优选告诉你...
  • koa2异常处理_读 koa2 源码后的一些思考与实践

使用python pandas dataframe学习数据分析相关推荐

  1. Python pandas.DataFrame.combine_first函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  2. Python pandas.DataFrame.tz_localize函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  3. Python pandas.DataFrame.expanding函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  4. Python pandas.DataFrame.melt函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  5. Python pandas.DataFrame.round函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  6. Python pandas.DataFrame.kurt函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  7. Python pandas.DataFrame.max函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  8. Python pandas.DataFrame.tail函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

  9. Python pandas.DataFrame.sum函数方法的使用

    Pandas是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速 ...

最新文章

  1. 算法训练 区间k大数查询
  2. VUE 路径拦截, 开放页面, 基于动态路由, 拦截器
  3. mysqld_multi stop 不能停掉mysql
  4. 软件测试面试题-接口测试
  5. nodejs 从gitlab拉下来后运行爆错解决
  6. JS原型链原理(链表)
  7. ruby array 额
  8. 分析网站速度和性能的最佳工具
  9. C/C++中的位运算
  10. java teechart怎么用_TeeChart for Java
  11. 中国移动微处理器CM32M101A介绍
  12. 如何让Bing(必应)快速收录你的网站
  13. java 生成word 分页,jsp转word + 分页
  14. 《黑客与画家》摘要读后感
  15. 安装教程:PostgreSQL + PostGIS + pgAdmin
  16. kali Linux隐藏IP教程,Kali Linux 设置动/静态IP地址以及修改DNS
  17. 计算机毕业设计ssm高校学生社团管理系统n4pcu系统+程序+源码+lw+远程部署
  18. 世界杯网页梦幻联动.html
  19. ios app 解决微信扫二维码不能跳转问题
  20. 对手在开拓,苹果在“堕落”,创新路上,苹果还能走多远

热门文章

  1. ##连接符和#符的使用
  2. 我们究竟还要学习哪些Android知识?完整版开放下载
  3. 国内互联网公司算法机器学习岗(阿里星)面试总结
  4. Linux中强大的输入输出重定向和管道
  5. php 常用正则运算
  6. QBXT Day 5图论相关
  7. lock和synchronized的同步区别与选择
  8. springmvc常用注解之@Controller和@RequestMapping
  9. 怎样配置键盘最方便,以及一些设计的思考
  10. PyCharm调试错误