from pandas import Series,DataFrame
import pandas as pd
import numpy as np
行用0,列用1
(一) Series
一种类似一维数组的对象,由一组数据(各种numpy数据类型)以及一组与之相关的数据标签(即索引)组成
1.创建series
Obj1=Series([1,2,3,4]) [结果] 0 1 1 2 2 3 3 4 dtype: int64 |
#指定索引的情况 [结果] one 1 two 2 three 3 four 4 dtype: int64 值 [1 2 3 4] 值类型 <class 'numpy.ndarray'> 索引 Index(['one', 'two', 'three', 'four'], dtype='object') 索引类型 <class 'pandas.core.indexes.base.Index'> |
sdata={ 'Ohio':35000,'Texas':71000,'Oregon':16000,'Utah':5000} obj3=Series(sdata) 【结果】 Ohio 35000 Oregon 16000 Texas 71000 Utah 5000 dtype: int64 |
states=['California','Ohio','Texas','Oregon','Utah'] obj4=Series(sdata,index=states) 【结果】 California NaN Ohio 35000.0 Texas 71000.0 Oregon 16000.0 Utah 5000.0 dtype: float64 |
2.索引
索引取值 print(obj3['Ohio']) |
Numpy数组运算(布尔运算、标量乘法、数学函数)等,保留对于索引和值得对应关系 Series利用标签的切片运算与普通的Python切片运算不同,其末端是包含的 |
算数运算中自动对齐索引 print(obj3+obj4) 【结果】 California NaN Ohio 70000.0 Oregon 32000.0 Texas 142000.0 Utah 10000.0 dtype: float64 |
Series及索引命名 obj5.name='popultion' 【结果】 state California NaN Ohio 70000.0 Oregon 32000.0 Texas 142000.0 Utah 10000.0 Name: popultion, dtype: float64 |
索引可以就地修改 obj1.index=['aaa','bbb','ccc','ddd'] 【结果】 aaa 1 bbb 2 ccc 3 ddd 4 dtype: int64 |
3.缺失数据
NaN表示缺失数据
Obj.isnull()返回布尔型
(二) DataFrame
DataFrame中的数据是以一个或多个二维块存放的
DataFrame 面向行和面向列的操作基本是平衡的,任意抽出一列都是 Series。
1.创建
直接传入一个等长列表或者NumPy数组组成的字典
data={
'state':['California','Ohio','Texas','Oregon','Utah'], |