Pandas入门系列(一)-- Series

Series的创建

##数据分析汇总学习

https://blog.csdn.net/weixin_39778570/article/details/81157884

# 使用列表创建

>>> import numpy as np
>>> import pandas as pd
>>> s1 = pd.Series([1,2,3,4])
>>> s1
0    1
1    2
2    3
3    4
dtype: int64
# 查看s1的值和索引
>>> s1.values
array([1, 2, 3, 4], dtype=int64)
>>> s1.index
RangeIndex(start=0, stop=4, step=1) # 默认索引

# 使用数组创建

>>> s2 = pd.Series(np.arange(10))
>>> s2
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
dtype: int32

# 使用字典创建

>>> s3 = pd.Series({'1':1, '2':2, '3':3})
>>> s3
1    1
2    2
3    3
dtype: int64
>>> s3.values
array([1, 2, 3], dtype=int64)
>>> s3.index
Index(['1', '2', '3'], dtype='object')

Series的访问

>>> s4 =  pd.Series([1,2,3,4], index = ['a','b','c','d'])
>>> s4
a    1
b    2
c    3
d    4
dtype: int64
>>> s4.values
array([1, 2, 3, 4], dtype=int64)
>>> s4.index
Index(['a', 'b', 'c', 'd'], dtype='object')
>>> s4['a'] # 访问索引为a的值
1
>>> s4[s4>2] #访问s4中值大于2的Series
c    3
d    4
dtype: int64

# Series与字典的转换

>>> s4.to_dict()  # s4转换为字典
{'a': 1, 'b': 2, 'c': 3, 'd': 4}


>>> s5 = pd.Series(s4.to_dict())  # 字典转换为Series
>>> s5
a    1
b    2
c    3
d    4
dtype: int64

# e索引无值补充为NaN

 

>>> index_1 = ['a','b','c','d','e']
>>> s6 = pd.Series(s5, index = index_1)
>>> s6
a    1.0
b    2.0
c    3.0
d    4.0
e    NaN  # s5此处无值
dtype: float64

# NaN判断

 

>>> pd.isnull(s6)
a    False
b    False
c    False
d    False
e     True
dtype: bool
>>> pd.notnull(s6)
a     True
b     True
c     True
d     True
e    False
dtype: bool

# 命名修改

 

>>> s6.name = 'demo'   # s6的名字修改
>>> s6
a    1.0
b    2.0
c    3.0
d    4.0
e    NaN
Name: demo, dtype: float64

>>> s6.index.name = 'demo_index'  # s6的索引的名字的修改
>>> s6.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object', name='demo_index')



官网:http://pandas.pydata.org/pandas-docs/version/0.14.1/

 

 

 

 

 

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值