学习 pandas [02]: 二进制操作

title: 学习 pandas [02]: 二进制操作 slug: er-jin-zhi-cao-zuo date: 2021-1-23 tags: Pandas Jupyter python category: 数据分析 link: description: type: text 二进制操作 Pandas数据结构之间执行二进制操作,要注意下列两个关键点: DataFrame与Series之间的广播机制; 计算中的缺失值处理。 加、减、乘、除、取模 DataFrame支持以下运算操作中对Series进行广播: add():加 sub():减 mul():乘 div():除 radd() rsub() 在以上方法使用中,通过axis参数,指定沿index或columns广播。 ...

2021年1月23日 · 4 分钟 · tsingk

学习 pandas [01]: 数据结构

title: 学习 pandas [01]: 数据结构 slug: series-and-dataframe date: 2020-12-27 tags: Pandas Jupyter python category: 数据分析 link: description: type: text 数据结构 pandas的数据类型主要有: Series,类似一维数组 DataFrame,类似二维数组 Series 创建Series 1 pd.Series(data, index=index) data:可以是字典、一维数组、标量 index:索引列表,可以理解为行标签,根据Data的类型而不同。 通过字典创建Series 1 2 3 4 5 6 7 import numpy as np import pandas as pd # 通过字典创建,自动采用键值作为索引,若不指定index,则顺序同字典 d = {'b': 1, 'a': 0, 'c': 2} s = pd.Series(d) s b 1 a 0 c 2 dtype: int64 1 2 3 4 # 通过字典创建,自动采用键值作为索引,若指定index(包含键值),则顺序同index # index中若含有字典中不存在的键值,则Series中对应的值为NaN s = pd.Series(d, index=['a', 'b', 'c', 'd']) s a 0.0 b 1.0 c 2.0 d NaN dtype: float64 通过一维数组创建Series 1 2 3 4 5 l = np.random.randn(6) # 若不指定index,则自动生成数值型索引 s = pd.Series(l) s 0 0.215224 1 0.803700 2 1.141428 3 0.526450 4 0.577533 5 1.051918 dtype: float64 1 2 3 # 若指定index,则长度必须与数组相同 s = pd.Series(l, index=['a', 'b', 'c', 'd', 'e', 'f']) s a 0.215224 b 0.803700 c 1.141428 d 0.526450 e 0.577533 f 1.051918 dtype: float64 通过标量值创建Series 此方法创建出的Series,每行值都相同。 ...

2020年12月27日 · 4 分钟 · tsingk

利用 AkShare 获取上证指数

AkShare 介绍 AkShare 是基于 Python 的财经数据接口库, 目的是实现对股票、期货、期权、基金、外汇、债券、指数、加密货币等金融产品的基本面数据、实时和历史行情数据、衍生数据从数据采集、数据清洗到数据落地的一套工具,主要用于学术研究目的。 可以编写 Python 代码,利用 AkShare 自动化获取股票、基金等金融数据。 项目文档:https://www.akshare.xyz/zh_CN/latest/index.html ...

2020年11月12日 · 2 分钟 · tsingk

练手 Matplotlib | 西咸地区总人口变化趋势

起因 之前分析过近年来西安市和陕西省的人口变化趋势,发现西安市人口不断在增长,陕西省人口数量基本持平。 昨天无意中看到一个说法,西安市和咸阳市总人口数量近年来基本没变化,于是想验证下是否如此,顺便练手 matplotlib 做图。 采用常住人口数据,2018年之前数据来自《陕西区域统计年鉴2018》,2018年数据来自相应省市的《2018年国民经济和社会发展统计公报》。 做图 人口数据保存在 .csv 文件里,所以先读取。 ...

2019年10月31日 · 2 分钟 · tsingk

练手 Matplotlib | 西安人口数量变化

Matplotlib练手1 用Matplotlib做图,可视化近几年西安常住人口数量变化。 数据来源 所用数据来自《陕西区域统计年检 2018》(2000 年~2017年数据)、2018 年陕西省国民经济和社会发展统计公报(2018年数据)、2018 年西安市国民经济和社会发展统计公报(2018年数据)。 绘图 先采用折线图看下西安市与陕西省近几年的常住人口数量变化。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 %matplotlib inline import matplotlib.pyplot as plt from pylab import mpl mpl.rcParams['font.sans-serif'] = ['simhei'] # 指定默认字体 mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 mpl.rcParams['figure.figsize'] = (8.0, 6.0) y = [2000, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018] people_xian = [688.01, 806.81, 822.52, 830.54, 837.52, 843.46, 847.41, 851.34, 855.29, 858.81, 862.75, 870.56, 883.21, 961.67, 1000.37] people_shanxi = [3644, 3690, 3699, 3708, 3718, 3727, 3735, 3743, 3753, 3764, 3775, 3793, 3813, 3835, 3864.40] p1, = plt.plot(y, people_xian) p2, = plt.plot(y, people_shanxi) plt.xlabel('年份') plt.ylabel('常住人口数/万人') plt.xticks(range(2000, 2019, 1), rotation=60) plt.legend([p1, p2], ["西安市", "陕西省"], loc=0) plt.show() ...

2019年7月30日 · 2 分钟 · tsingk