pandas-02 Series()和DataFrame()的区别与联系

区别:

series,只是一个一维数据结构,它由index和value组成。

dataframe,是一个二维结构,除了拥有index和value之外,还拥有column。

联系:

dataframe由多个series组成,无论是行还是列,单独拆分出来都是一个series。

代码演示:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame data = {'Country':['Belgium', 'India', 'Brazil'],
'Capital':['Brussels', 'New Delhi', 'Brasilia'],
'Population':[11190846, 1303171035, 207847528]
} # Series s1 = Series(data['Country'])
print(s1)
'''
0 Belgium
1 India
2 Brazil
dtype: object
'''
print(s1.values) # 类型: <class 'numpy.ndarray'>
'''
['Belgium' 'India' 'Brazil']
'''
print(s1.index)
'''
RangeIndex(start=0, stop=3, step=1)
''' # 为Series指定index
s1 = Series(data['Country'], index=['A', 'B', 'C'])
print(s1)
''' 索引更改
A Belgium
B India
C Brazil
dtype: object
''' # Dataframe df1 = pd.DataFrame(data)
print(df1)
'''
Capital Country Population
0 Brussels Belgium 11190846
1 New Delhi India 1303171035
2 Brasilia Brazil 207847528
''' print(df1['Capital']) # 类型: series
'''
0 Brussels
1 New Delhi
2 Brasilia
Name: Capital, dtype: object
''' print(df1.iterrows()) # 返回 一个 生成器 <generator object DataFrame.iterrows at 0x7f226a67b728> for row in df1.iterrows():
print(row)
print(row[0], row[1])
print(type(row[0]), type(row[1]))
break
'''
print(row) 返回了一个元组
(0, Capital Brussels
Country Belgium
Population 11190846
Name: 0, dtype: object)
'''
'''
print(row[0], row[1]) 的返回值
0 Capital Brussels
Country Belgium
Population 11190846
Name: 0, dtype: object
'''
'''
print(type(row[0]), type(row[1]))
<class 'int'> <class 'pandas.core.series.Series'> row[1] 是一个 series,而且原来的列名,现在变成了现在的索引名,
由此可见,dataframe是由多个行列交错的series组成。
''' # 现在可以 构建几个series
s1 = pd.Series(data['Country'])
s2 = pd.Series(data['Capital'])
s3 = pd.Series(data['Population'])
df_new = pd.DataFrame([s1, s2, s3], index=['Country', 'Captital', 'Population'])
print(df_new)
'''
0 1 2
Country Belgium India Brazil
Captital Brussels New Delhi Brasilia
Population 11190846 1303171035 207847528 可以看到,行 和 列 都是颠倒的,因此需要进行一下转置
''' print(df_new.T)
'''
Country Captital Population
0 Belgium Brussels 11190846
1 India New Delhi 1303171035
2 Brazil Brasilia 207847528 ''' '''
总结:
series, 就是一个 一维 的数据结构,它是由 index 和 value 组成。
dataframe, 是一个 二维 数据结构,它由多个 series 构成。
'''

pandas-02 Series()和DataFrame()的区别与联系的更多相关文章

  1. Pandas中Series与Dataframe的区别

    1. Series Series通俗来讲就是一维数组,索引(index)为每个元素的下标,值(value)为下标对应的值 例如: arr = ['Tom', 'Nancy', 'Jack', 'Ton ...

  2. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  3. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  4. Python之Pandas中Series、DataFrame实践

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  5. Pandas中Series和DataFrame的索引

    在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引.比如list进行索引的时候使用的是下标,而dict索引的时候使用的是关键字. ...

  6. 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作

    一.reindex() 方法:重新索引 针对 Series   重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...

  7. pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  8. pandas学习series和dataframe基础

    PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...

  9. [Python] Pandas 中 Series 和 DataFrame 的用法笔记

    目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...

随机推荐

  1. JCR分区 | 中科院SCI期刊分区表

    LetPub查询系统,非常方便,分区影响因子都可以查询,还有投稿经验可以参考. SCI全称是Science Citation Index(科学引文索引) 科睿唯安JCR分区(Journal Citat ...

  2. Myeclipse安装Maven插件

    Myeclipse安装Maven插件 一.下载Maven 官网下载maven插件  http://maven.apache.org/download.cgi 下载apache-maven-3.6.3- ...

  3. 基于redis5的session共享:【redis 5.x集群应用研究】

    基于springsession构建一个session共享的模块. 这里,基于redis的集群(Redis-5.0.3版本),为了解决整个物联网平台的各个子系统之间共享session需求,且方便各个子系 ...

  4. Flutter Plugin开发简单示例

    新建Plugin项目: flutter create --template=plugin -i swift -a javahello lib/hello.dart: 插件包的Dart API. and ...

  5. android mk 预编译库

    LOCAL_PATH := $(call my-dir) #include $(CLEAR_VARS) # OpenCV #OPENCV_CAMERA_MODULES:=on #OPENCV_INST ...

  6. 搭建Portainer可视化界面(转)

    转载地址:https://blog.csdn.net/u011781521/article/details/80469804 一.什么是Portainer? Portainer是Docker的图形化管 ...

  7. asp.net msbuild 发布

    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\msbuild.exe ...

  8. 爬取的地址存入mysql记录

    CREATE DATABASE HELLO; CREATE TABLE IF NOT EXISTS `botoo`( `id` INT UNSIGNED AUTO_INCREMENT, `title` ...

  9. OpenShift 4.2环境离线部署Operatorhub

    缺省离线环境安装的ocp4的Operatorhub是没有内容的.详细离线文档参考官网文档 https://docs.openshift.com/container-platform/4.2/opera ...

  10. [Linux.centOS].安装Redis 腾讯云

    环境 { "服务器运营商":"腾讯云", "操作系统":"CentOS 7.5 64位", "CPU" ...