python进阶-10.pandas基本数据类型(1)

import pandas as pdimport numpy as npfrom pandas import Series,DataFrame1,Seriespandas.Series() 返回一个有 index 和 values 属性的数对象s=pd.Series([4,5,-7,3])s0 41 52 -73 3dtype: int64s.indexRangeIndex(start=0, stop=4, step=1)s.valuesa
摘要由CSDN通过智能技术生成

import pandas as pd
import numpy as np
from pandas import Series,DataFrame

1,Series

  • pandas.Series() 返回一个有 index 和 values 属性的数对象
s=pd.Series([4,5,-7,3])
s
0    4
1    5
2   -7
3    3
dtype: int64
s.index
RangeIndex(start=0, stop=4, step=1)
s.values
array([ 4,  5, -7,  3], dtype=int64)

自定义索引

s1=Series([4,7,6,5,1],index=['a','b','c','d','e'],dtype=float)
s1
a    4.0
b    7.0
c    6.0
d    5.0
e    1.0
dtype: float64
s1.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

读取 Series

  • 1,使用 0-n 的索引
  • 2,自定义的索引
  • #数字索引读取
s1[0]
4.0
  • #自定义索引读取
s1['a']
4.0
  • 如果自定义索引是纯数字,就不能用 0 -n 的索引读取了,只能用自定义
s2=Series([4,7,6,5], index=[3,5,7,9],dtype=np.float64)
s2
3    4.0
5    7.0
7    6.0
9    5.0
dtype: float64
s2[3]
4.0

Sereis.loc[‘自定义的索引’] || Sereis.iloc[0-n的数字]

s2.loc[7]
6.0
s2.iloc[2]
6.0

xxx.loc[‘yy’]=new 修改和增加

s1
a    4.0
b    7.0
c    6.0
d    5.0
e    1.0
dtype: float64
s1.loc['e']=1
s1
a    4.0
b    7.0
c    6.0
d    5.0
e    1.0
dtype: float64
</
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值