python学习笔记六:Series

Series是一种类似于一维数组的对象,由以下两部分组成:
values:一组数据(ndarray或list类型)
index:相关的数据索引标签

一维数组默认索引为0,1,2,3...,而Series的索引可以自己给定,比如说统计班级同学身高时,用0,1,2,3...索引不明了,可以直接将每个人的名字当作索引,所以Series是一维数组的升级。
特别地,由ndarray创建的是引用,而不是副本。对Series元素的改变也会改变原来ndarray对象中的元素。(列表没有这种情况)
一维

#导入三剑客:numpy;pandas;matplotlib
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
import matplotlib.pyplot as plt

1.通过list,ndarray和 dict创建Series

#Series存储的数据必须是一维的,Data must be 1-dimensional
#通过列表创建序列
s = Series([1,3,5,7])
print(s)
s.index = list('abcd')
print(s)

#通过ndarray创建序列
nd = np.random.randint(160,180,size = 4)
s1 = Series(nd,index = ['张三','李四','王五','赵六'])
print(s1)

#通过字典创建序列
s2 = Series({'a':1,'b':3,'c':5,'d':7})
print(s2)

2.Series的索引和切片

可以使用中括号取单个索引(此时返回的是元素类型),或者中括号里一个列表取多个索引(此时返回的仍然是一个Series类型)。分为显式索引和隐式索引:

s = Series(np.random.random(10),index = list('abcdefghij'))

#显式索引
print(s['a'])
print(s.loc['c'])

#隐式索引
print(s[0])
print(s.iloc[2])
#切片
print(s[0:2])
print(s.loc['a':'c'])

3.Series的基本概念

当索引没有对应值时,可能缺失数据显示NaN(not a number)的情况

print(s.shape,s.size,s.values,s.index)#查看形状,大小,元素值,索引值
s.head()#查看前部分
s.tail()#查看后部分

s = Series([1,2,None,np.nan],index = list('abcd'))
print(s)
print(s.sum())

#检测缺失数据
s1 = s.isnull()
s2 = s.notnull()
s[s2]#会将True的元素全部提取出来
#name用于区分,DataFrame中用于区分,在DataFrame中是列名
s.name = '牛逼'
print(s)
#在进行线性运算时,如果包含Nan,那么fill_value默认将Nan设置为=后面的值
s.add(10,fill_value=0)

4.Series的线性运算

#两个Series进行相加时,就是索引相同进行相加
s1 = Series([2,4,7,9],index=[0,1,2,3])
s2 = Series([1,2,3,4],index=[2,3,4,5])
s1 + s2
运行结果:
0     NaN
1     NaN
2     8.0
3    11.0
4     NaN
5     NaN

#使用add函数时,相加后每个索引都有值
s1.add(s2,fill_value=0)
运行结果:
0     2.0
1     4.0
2     8.0
3    11.0
4     3.0
5     4.0

 

数1组默认索引为0,1,2,3...,而Se

 

一维数组默认索引为0,1,2,3...,而Series的索引可以自己给定,比如说统计班级同学身高时,用0,1,2,3...索引不好,可以直接将每个人的名字当所以Series是一维数组的升级。

特别地,由ndarray创建的是引用,而不是副本。对Series元素的改变也会改变原来ndarray对象中的元素。(列表没有这种情况)

ries的索引可以自己给定,比如说统计班级同学身高时,用0,1,2,3...索引不好,可以直接将每个人的名字当作索引,所以Serie幅是v点击地,由ndarray创建的是引用,而不是副本。对Series元素的改变也会改变原来ndarray对象中的元素。(列表没

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Artificial Intelligence is becoming increasingly relevant in the modern world where everything is driven by technology and data. It is used extensively across many fields such as search engines, image recognition, robotics, finance, and so on. We will explore various real-world scenarios in this book and you’ll learn about various algorithms that can be used to build Artificial Intelligence applications. During the course of this book, you will find out how to make informed decisions about what algorithms to use in a given context. Starting from the basics of Artificial Intelligence, you will learn how to develop various building blocks using different data mining techniques. You will see how to implement different algorithms to get the best possible results, and will understand how to apply them to real-world scenarios. If you want to add an intelligence layer to any application that’s based on images, text, stock market, or some other form of data, this exciting book on Artificial Intelligence will definitely be your guide! What You Will Learn Realize different classification and regression techniques Understand the concept of clustering and how to use it to automatically segment data See how to build an intelligent recommender system Understand logic programming and how to use it Build automatic speech recognition systems Understand the basics of heuristic search and genetic programming Develop games using Artificial Intelligence Learn how reinforcement learning works Discover how to build intelligent applications centered on images, text, and time series data See how to use deep learning algorithms and build applications based on it Table of Contents Chapter 1. Introduction to Artificial Intelligence Chapter 2. Classification and Regression Using Supervised Learning Chapter 3. Predictive Analytics with Ensemble Learning Chapter 4. Detecting Patterns with Unsupervised Learning Chapter 5. Building Recommender Systems Chapter 6. Logic Programming Chapter 7. Heuri

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值