Pandas学习笔记(1)

一、Pandas的数据结构介绍
>>> from pandas import Series,DataFrame
>>> import pandas as pd
>>> import numpy as np
1.Series
Series:类似于一维数组的对象,由一组数据(各种numpy的数据类型)以及一组与之相关的数据标签(即索引)组成
>>> obj=Series([1,2,3,4])
#如果不指定索引,会自动生成从0-(N-1)的整数型索引
>>> obj
0    1
1    2
2    3
3    4
dtype: int64
>>> obj.values
array([1, 2, 3, 4])
>>> obj.index
RangeIndex(start=0, stop=4, step=1)
#numpy数组运算保留索引和值之间的关系
>>> obj[obj>2]
2    3
3    4
dtype: int64
>>> obj*2
0    2
1    4
2    6
3    8
dtype: int64
>>> np.exp(obj)
0     2.718282
1     7.389056
2    20.085537
3    54.598150
dtype: float64
#如果数据被存放在有一个python字典中,也可以直接通过这个字典创建Series
>>> score={"Tom":99,"Lucy":90,"John":80,"Green":58}
>>> score
{'John': 80, 'Green': 58, 'Lucy': 90, 'Tom': 99}
>>> obj_score=Series(score)
>>> obj_score
Green    58
John     80
Lucy     90
Tom      99
dtype: int64
#Series可以被看成是一个定长的有序字典,可以用很多原本需要字典参数的函数
>>> "Green" in obj_score
True
>>> "yaoxq" in obj_score
False
#将一个字典传入Series的索引,就可以得到匹配的值,“NaN”表示缺失或者NA值。
>>> name={"A","B","C","Tom"}
>>> obj_score_new=Series(obj_score,index=name)
>>> obj_score_new
A       NaN
C       NaN
B       NaN
Tom    99.0
dtype: float64
#我们可以使用isnull和isnotnull来检测缺失数据
>>> pd.isnull(obj_score)
Green    False
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值