ohio

pandas的基本功能

早过忘川 提交于 2020-03-08 19:04:48
一、重建索引 (一)reindex(行索引) 1、是pandas对象的重要方法,用于创建一个符合新索引的新对象,注意:Series调用reindex方法时,会将数据按照新的索引进行排列,如果某个索引值之前并不存在,则会引入缺失值。 import pandas as pd passion=pd.Series([4.5,7.2,-5.3,3.6],index=[‘d’,‘b’,‘a’,‘c’]) print(passion) passion1=passion.reindex([‘a’,‘b’,‘c’,‘d’,‘e’]) print(passion1) 返回值为 d 4.5 b 7.2 a -5.3 c 3.6 dtype: float64 a -5.3 b 7.2 c 3.6 d 4.5 e NaN dtype: float64 2、插值:在重建索引时需要插值的,method可选参数允许使用ffill方法会将值前向填充,其中要求索引必须是数字,而且必须是递增或递减的顺序。 import pandas as pd passion=pd.Series([4.5,7.2,-5.3,3.6],index=[‘0’,‘2’,‘4’,‘6’]) print(passion) passion1=passion.reindex([‘0’,‘1’,‘2’,‘4’,‘6’],method=‘ffill’)

Pandas 之 Series / DataFrame 初识

北慕城南 提交于 2019-12-04 14:01:26
import numpy as np import pandas as pd Pandas will be a major tool of interest throughout(贯穿) much of the rest of the book. It contains data structures and manipulation tools designed to make data cleaning(数据清洗) and analysis fast and easy in Python. pandas is often used in tandem(串联) with numerical computing tools like NumPy and SciPy, analytical libraries like statsmodels and scikit-learn, and data visualization(可视化) libraries like matplotlib. pandas adopts(采用) sinificant(显著的,大量的) parts of NumPy's idiomatic(惯用的) style of array based computing, especially array-based functions and preference

python之padnas学习(四)

匿名 (未验证) 提交于 2019-12-02 22:11:45
A.整数索引: 用整数索引在pandas中 与python的list与truple有一点不同,如果在Series的默认row Index为数字的话,用整数索引会出错,那么我们就应该将index更改掉,改成其它。 1. import numpy as np import pandas as pd obj=pd.Series(np.arange(3.)) print(obj) print(obj[-1]) 看得出来如果这样会报错! 因为在我们创建Series的时候index 为0,1,2 .而我们obj[-1] 表达的意思和它起冲突了,所以程序想知道我们想要什么就困难。我们需要的倒数第一个,而它以为我们是要index为-1的这个数据,所以就会报错。那么我们创建时将index更改掉,那么就可以了! obj1=pd.Series(np.arange(3.),index=['a','b','c']) print(obj1[-1]) 2. 其实也可以这样索引,从输出中看出每个的不同,这里我用的是创建的第一个对象obj,不是obj1 print(obj[:1]) print(obj.loc[:1]) print(obj.iloc[:1]) B.算数与数据对齐: padnas一个特点是,两个相同的数据结构相加,那么他们的索引值对应相加,如果这两个,其中一个有不同的index,那么相加后

NumPy基本操作快速熟悉

好久不见. 提交于 2019-11-27 05:31:34
本文包含一些 Pandas 的基本操作,旨在快速上手 Pandas 的基本操作。 读者最好有 NumPy 的基础,如果你还不熟悉 NumPy,建议您阅读 NumPy基本操作快速熟悉 。 Pandas 数据结构 Pandas 有两个核心的数据结构: Series 和 DataFrame 。 Series Series 是 一维的类数组对象 ,包含一个 值序列 以及对应的 索引 。 1 obj = pd.Series([6, 66, 666, 6666]) 2 obj 0 6 1 66 2 666 3 6666 dtype: int64 此时索引默认为 0 到 N。我们可以分别访问 Series 的值和索引: 1 obj.values 2 obj.index # 类似于 range(4) array([ 6, 66, 666, 6666]) RangeIndex(start=0, stop=4, step=1) 索引可以用标签来指定: 1 obj2 = pd.Series([6, 66, 666, 6666], index=['d', 'b', 'a', 'c']) 2 obj2 3 obj2.index d 6 b 66 a 666 c 6666 dtype: int64 Index(['d', 'b', 'a', 'c'], dtype='object') 可以使用 标签索引