惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
罗磊的独立博客
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX

老董笔记

尚硅谷机构在哪?尚硅谷培训怎么样?靠谱吗-互联网IT百科 韩顺平介绍,传智讲师,开办泰牛,入尚硅谷等一系列-互联网IT百科 pandas多重索引标准样式(写入excel有空行)-互联网IT百科 cannot join with no overlapping index names-互联网IT百科 pandas多列变多行(即宽表变长表)melt和stack函数-互联网IT百科 pandas多行转多列(长表变宽表)pivot和unstack-互联网IT百科 Index contains duplicate entries, cannot reshape完美解决-互联网IT百科 single positional indexer is out-of-bounds-互联网IT百科 Can only compare identically-labeled Series objects-互联网IT百科 pandas transform用法详解(多个案例)-互联网IT百科 python四舍五入精确实现-互联网IT百科 pandas的groupby使用apply分组排序-互联网IT百科 index 0 is out of bounds for axis 0 with size 0-互联网IT百科 pandas分组过滤filter函数-互联网IT百科 联想Win10系统如何禁用触摸屏关闭触摸-互联网IT百科 groupby分组计算transform转换返回相同长度序列-互联网IT百科 brooks seo教程python教程,brooks seo教程网盘,布鲁seo资源-互联网IT百科 电脑右键文件夹一直转圈电卡死怎么回事-互联网IT百科 施琪嘉的心理成长课(荐)-互联网IT百科 百度SEO公司_SEO推广公司哪家好_SEO外包服务如何选-老董笔记 groupby后agg同1列用多个聚合函数、不同列用不同函数、自定义函数-互联网IT百科 pandas的groupby单列多列分组聚合运算-互联网IT百科 DataFrameGroupBy对象及分组个数、分组大小、组名索引、组数据详解-互联网IT百科 pandas中groupby之Grouper and axis must be same length-互联网IT百科 pandas中groupby的分组原理-互联网IT百科 pandas的groupby的使用详解大全-互联网IT百科 openpyxl单元格自动换行强制换行Alignment(wrapText=True)-互联网IT百科 python教程全套(可就业)-互联网IT百科 联想win10系统CPU显示100%,电脑呼呼响怎么回事-互联网IT百科 如何自制CPU,CPU原理是怎么样的?-互联网IT百科
Series创建,name,index(索引),values(值),dtype等属性-互联网...
2020-11-17 · via 老董笔记

  数组:

  数组的概念可以看做是一群数据的集合,有点类似python的列表、元组,但是并不相同。在pandas中提到数组一般指的是ndarray(numpy中的数组)

  Series数据结构

  1、Series是一个一维数组(带有索引,索引可以为数值也可以为标签(比如a、b、c)),能够保存任何数据类型(整数、字符串、浮点数、Python对象等)。

  2、Series和DataFrame都有索引,只不过DataFrame同时有列和行索引。

  3、Series相比ndarray多了一个索引,相当于一位数组+索引。

  4、Series和dict相比,Series更像有顺序的字典,其中字典的key就是索引,索引可以是数字也可以是字母。

  5、Series有索引,也有name属性(可以理解为excel的一列的表头)

  6、只看Series的值,就是ndarray数组

  Series创建(通过字典、数组、标量):

  1、通过字典创建,字典的键就是索引,值就是值。也可以人为指定了index(索引),如果指定的index没有对应的值,那么就会用缺失值(空值)替代。

# -*- coding: utf-8 -*-

import pandas as pd


# 通过dict创建
d = {'A': 1, 'B': 0, 'C': 2}
s = pd.Series(d)
print(s)
print('==================')
s2 = pd.Series(d, index=['A', 'B', 'c', 'd'])
print(s2)

A    1
B    0
C    2
dtype: int64
==================
A    1.0
B    0.0
c    NaN
d    NaN
dtype: float64

  2、通过数组(np数组、列表、元组等)创建

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

# 通过数组创建
arr = np.random.rand(5)
s = pd.Series(arr)
print(s)
print('============')
s2 = pd.Series(arr,index=['a','b',3,4,1])
print(s2)
0    0.543199
1    0.261755
2    0.464825
3    0.119778
4    0.436607
dtype: float64
============
a    0.543199
b    0.261755
3    0.464825
4    0.119778
1    0.436607
dtype: float64

  3、通过标量创建

  index索引值可以出现相同的,但是应避免这样

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

# 通过标量创建
s = pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])
print(s)
a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
dtype: float64

  name属性及rename方法:

  1、Series的name属性(相当于excel的一列添加了表头名)

  2、rename方法修改name

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

# 通过标量创建
s = pd.Series(5., index=['a', 'b', 'c', 'd', 'e'],name = '高度')
print(s)
print('-----------')
s.name = '长度'
print(s)
print('-----------')
s = s.rename('宽度')
print(s)
a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
Name: 高度, dtype: float64
-----------
a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
Name: 长度, dtype: float64
-----------
a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
Name: 宽度, dtype: float64

  Series的index属性(获取索引)、values属性(获取值)

# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd

arr = np.random.rand(3)
s = pd.Series(arr)
print('s的索引',s.index,type(s.index))
print('把s索引变为列表',list(s.index))
print('s的值',s.values,type(s.values))
print('把s的值变为列表',list(s.values))
s的索引 RangeIndex(start=0, stop=3, step=1) <class 'pandas.core.indexes.range.RangeIndex'>
把s索引变为列表 [0, 1, 2]
s的值 [0.86401824 0.86229577 0.4742213 ] <class 'numpy.ndarray'>
把s的值变为列表 [0.8640182359257331, 0.8622957709758832, 0.4742213037368106]

  我们来综合看一下series的常见属性,其中dtype和dtypes相对比较复杂,我们后面会专门解释:

1 axes 返回行轴标签列表。
2 dtype 返回对象的数据类型(dtype)。
3 dtypes 返回对象的数据类型(dtypes)。
4 empty 如果系列为空,则返回True。
5 ndim 返回底层数据的维数,默认定义:1。
6 size 返回基础数据中的元素数。
7 values 将系列作为ndarray返回。
8 head() 返回前n行。
9 tail() 返回最后n行。

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会