Series基本操作

简介: Series基本操作
import pandas as pd
s1 = pd.Series([i for i in range(1,6)])
  • 访问所有的索引
s1.index
RangeIndex(start=0, stop=5, step=1)
  • 访问所有的值
s1.values
array([1, 2, 3, 4, 5])
  • 传入ndarray
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: int64
  • 传入字典
s3 = pd.Series({'1': 1, '2': 2, '3': 3})
s3
1    1
2    2
3    3
dtype: int64
s3.index
Index(['1', '2', '3'], dtype='object')
s3.values
array([1, 2, 3])
  • 指定index
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])
s4.index
Index(['A', 'B', 'C', 'D'], dtype='object')
  • Series的访问
s4['A']
1
  • Series的筛选
s4[s4>2]
C    3
D    4
dtype: int64
  • 转化为字典
s4.to_dict()
{'A': 1, 'B': 2, 'C': 3, 'D': 4}
# 如果只传入一个字典,则结果Series中的索引就是原字典的键(有序排列)。你可以传入排好序的字典的键以改变顺序:
s5 = pd.Series(s4.to_dict())
s5
A    1
B    2
C    3
D    4
dtype: int64
index_1 = ['A', 'B', 'C','D','E']
s6 = pd.Series(s5, index = index_1)
s6 #由于E这个索引没有对应的值,即为NaN(即“非数字”(not a number),在pandas中,它用于表示缺失或NA值
A    1.0
B    2.0
C    3.0
D    4.0
E    NaN
dtype: float64
  • 我将使用缺失(missing)或NA表示缺失数据。pandas的isnull和notnull函数可用于检测缺失数据:
pd.isnull(s6)
A    False
B    False
C    False
D    False
E     True
dtype: bool
# Series也有类似的实例方法, 面向对象调用
s6.isnull()
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
  • Series对象本身及其索引都有一个name属性,该属性跟pandas其他的关键功能关系非常密切:
s6.name = 'DEMO1'
s6
A    1.0
B    2.0
C    3.0
D    4.0
E    NaN
Name: DEMO1, dtype: float64
s6.index.name = 'demo_index'
s6
demo_index
A    1.0
B    2.0
C    3.0
D    4.0
E    NaN
Name: DEMO1, dtype: float64
  • Series的索引可以通过赋值的方式就地修改:
s6.index
Index(['A', 'B', 'C', 'D', 'E'], dtype='object', name='demo_index')
import string
up_index = [i for i in string.ascii_lowercase[21:26]]
up_index
['v', 'w', 'x', 'y', 'z']
s6.index = up_index
s6
v    1.0
w    2.0
x    3.0
y    4.0
z    NaN
Name: DEMO1, dtype: float64
相关文章
|
3月前
|
BI 数据处理 索引
Pandas基本操作:Series和DataFrame(Python)
Pandas基本操作:Series和DataFrame(Python)
294 1
|
索引 Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(四)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(四)
|
1月前
|
索引 Python
numpy的基本操作
numpy的基本操作
numpy的基本操作
|
3月前
|
Python
使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序
【5月更文挑战第2天】使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序。示例代码展示了如何按'Name'和'Age'列排序 DataFrame。先按'Name'排序,再按'Age'排序。sort_values()的by参数接受列名列表,ascending参数控制排序顺序(默认升序),inplace参数决定是否直接修改原DataFrame。
82 1
|
3月前
|
SQL 数据采集 数据挖掘
Pandas DataFrame 基本操作实例100个
Pandas DataFrame 基本操作实例100个
185 1
|
11月前
|
SQL 机器学习/深度学习 数据挖掘
pandas数据结构(Series和DataFrame)
无可非议,pandas是Python最强大的数据分析和探索工具之一,因金融数据分析工具而开发,支持类似于SQL语句的模型,可以对数据进行增删改查等操作,支持时间序列分析,也能够灵活的处理缺失的数据。它含有使数据分析工作变得更快更简单的高级数据结构和操作工具。pandas是基于NumPy构建的,让以NumPy为中心的应用变得更加简单。这里所说的让pandas变得更快更简单的高级数据结构就是Series和DataFrame。要熟练使用pandas,首先得要熟悉它的这两个主要的数据结构:Series和DateFrame。
77 0
|
索引 Python
Pandas中Series的属性、方法、常用操作以及使用示例(一)
Pandas中Series的属性、方法、常用操作以及使用示例
|
索引 Python
Pandas中Series的属性、方法、常用操作以及使用示例(二)
Pandas中Series的属性、方法、常用操作以及使用示例(二)
|
索引 Python
Pandas中Series的属性、方法、常用操作以及使用示例(三)
Pandas中Series的属性、方法、常用操作以及使用示例(三)
|
索引 Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(三)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(三)