导入pandas

import pandas as pd
countries = ['Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda',
'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan',
'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus',
'Belgium', 'Belize', 'Benin', 'Bhutan', 'Bolivia'] life_expectancy_values = [74.7, 75. , 83.4, 57.6, 74.6, 75.4, 72.3, 81.5, 80.2,
70.3, 72.1, 76.4, 68.1, 75.2, 69.8, 79.4, 70.8, 62.7,
67.3, 70.6] gdp_values = [ 1681.61390973, 2155.48523109, 21495.80508273, 562.98768478,
13495.1274663 , 9388.68852258, 1424.19056199, 24765.54890176,
27036.48733192, 1945.63754911, 21721.61840978, 13373.21993972,
483.97086804, 9783.98417323, 2253.46411147, 25034.66692293,
3680.91642923, 366.04496652, 1175.92638695, 1132.21387981] #将普通数组转换为pandas数组

life_expectancy = pd.Series(life_expectancy_values)

gdp = pd.Series(gdp_values)

pandas数组和numpy数组有很多一样的操作:

(1) 截取部分

print life_expectancy[0]

# 结果:
74.7 print gdp[3:6]

# 结果:
3 562.987685
4 13495.127466
5 9388.688523
dtype: float64

(2) 循环:

for country_life_expectancy in life_expectancy:
  print 'Examining life expectancy {}'.format(country_life_expectancy) # 结果
Examining life expectancy 74.7
Examining life expectancy 75.0
Examining life expectancy 83.4
Examining life expectancy 57.6
Examining life expectancy 74.6
Examining life expectancy 75.4
...
Examining life expectancy 67.3
Examining life expectancy 70.6

(3) 常用函数:

print life_expectancy.mean()   # 求平均数
# 结果
72.87
print life_expectancy.std() # 求标准差
# 结果
6.21399947487
print gdp.max()          # 求最大值
# 结果
27036.4873319
print gdp.sum()                # 求和 
# 结果
182957.59833

(4) 向量化运算:

a = pd.Series([1, 2, 3, 4])
b = pd.Series([1, 2, 1, 2]) print a + b
# 结果
0 2
1 4
2 4
3 6
dtype: int64 print a * 2
# 结果
0 2
1 4
2 6
3 8
dtype: int64 print a >= 3
# 结果
0 False
1 False
2 True
3 True
dtype: bool print a[a >= 3]
# 结果
2 3
3 4
dtype: int64

pandas数组(pandas Series)-(1)的更多相关文章

  1. pandas数组(pandas Series)-(4)NaN的处理

    上一篇pandas数组(pandas Series)-(3)向量化运算里说到,将两个 pandas Series 进行向量化运算的时候,如果某个 key 索引只在其中一个 Series 里出现,计算的 ...

  2. pandas数组(pandas Series)-(3)向量化运算

    这篇介绍下有index索引的pandas Series是如何进行向量化运算的: 1. index索引数组相同: s1 = pd.Series([1, 2, 3, 4], index=['a', 'b' ...

  3. pandas数组(pandas Series)-(2)

    pandas Series 比 numpy array 要强大很多,体现在很多方面 首先, pandas Series 有一些方法,比如: describe 方法可以给出 Series 的一些分析数据 ...

  4. pandas数组(pandas Series)-(5)apply方法自定义函数

    有时候需要对 pandas Series 里的值进行一些操作,但是没有内置函数,这时候可以自己写一个函数,使用 pandas Series 的 apply 方法,可以对里面的每个值都调用这个函数,然后 ...

  5. python数据分析之pandas库的Series应用

    一.pandas的数据结构介绍 1. Series 1.1 Series是由一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据索引构成.仅由一组数据可产生最简单的Series. from p ...

  6. pandas中数据结构-Series

    pandas中数据结构-Series pandas简介 Pandas是一个开源的,BSD许可的Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具.Python与Pan ...

  7. pandas | 使用pandas进行数据处理——Series篇

    本文始发于个人公众号:TechFlow,原创不易,求个关注 上周我们关于Python中科学计算库Numpy的介绍就结束了,今天我们开始介绍一个新的常用的计算工具库,它就是大名鼎鼎的Pandas. Pa ...

  8. Pandas之:Pandas简洁教程

    Pandas之:Pandas简洁教程 目录 简介 对象创建 查看数据 选择数据 loc和iloc 布尔索引 处理缺失数据 合并 分组 简介 pandas是建立在Python编程语言之上的一种快速,强大 ...

  9. Pandas之:Pandas高级教程以铁达尼号真实数据为例

    Pandas之:Pandas高级教程以铁达尼号真实数据为例 目录 简介 读写文件 DF的选择 选择列数据 选择行数据 同时选择行和列 使用plots作图 使用现有的列创建新的列 进行统计 DF重组 简 ...

随机推荐

  1. BigDecimal去除末尾多余的0

    Java有自带的 stripTrailingZeros() 方法用于去除末尾多余的0 BigDecimal num = new BigDecimal("100.000"); Big ...

  2. jenkins+maven+git+ 邮件自动转发 持续化集成 图文教程

    1.所需要的插件,安装plugin ,进入mangae Jenkins→ manage Plugins, 切换到Available tab, 选择如下plugin 安装 Gitplugin, GitH ...

  3. 倒计时实现两种方法-NSTimer/GCD

    #import "ViewController.h" @interface ViewController () @property (nonatomic ,strong)UIBut ...

  4. 浅析iOS中的触摸事件

    一.什么是响应者对象? 在 iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象”.UIApplication.UIViewControl ...

  5. 【代码导读】Github 开源项目——wysihtml5 富编辑器(Bootstrap 风格)【一】

    如果你经常留迹于各大论坛.博客,肯定对它们的富编辑器稍有印象.纯 Javascript 富编辑器可以说是前台 JS 脚本的巅峰作品.一款完整的编辑器,其复杂的功能,会让你遇到各种头痛的浏览器兼容问题, ...

  6. Python读文本文件中文乱问题

    file_object = open('thefile.txt')try: all_the_text = file_object.read().decode("gb2312")fi ...

  7. C# 禁止datagridview 自动产生列

    dataGridView1.AutoGenerateColumns = false;

  8. ORA-14404: partitioned table contains partitions in a different tablespace

    SQL> drop tablespace nn_data including contents and datafiles; drop tablespace nn_data including ...

  9. 10分钟轻松设置出 A+ 评分的 HTTP/2 网站

    前言 其实 HTTP/2 应该是 2015 年的老话题了(2015 年 5 月 14 日 HTTP/2 协议正式版的发布),但是 2018 年都到了很多网站依旧没有使用,作为新一代互联网协议,HTTP ...

  10. python os.chdir() 用法

    概述 os.chdir() 方法用于改变当前工作目录到指定的路径. 语法 chdir()方法语法格式如下: os.chdir(path) 参数 path -- 要切换到的新路径. 返回值 如果允许访问 ...