pandas-18 reindex用法
pandas-18 reindex用法
pandas中的reindex方法可以为series和dataframe添加或者删除索引。
方法:serise.reindex()、dataframe.reindex()
如果新添加的索引没有对应的值,则默认为nan。如果减少索引,就相当于一个切片操作。
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
np.random.seed(666)
# series reindex
s1 = Series([1, 2, 3, 4], index=['A', 'B', 'C', 'D'])
print(s1)
'''
A 1
B 2
C 3
D 4
dtype: int64
'''
# 重新指定 index, 多出来的index,可以使用fill_value 填充
print(s1.reindex(index=['A', 'B', 'C', 'D', 'E'], fill_value = 10))
'''
A 1
B 2
C 3
D 4
E 10
dtype: int64
'''
s2 = Series(['A', 'B', 'C'], index = [1, 5, 10])
print(s2)
'''
1 A
5 B
10 C
dtype: object
'''
# 修改索引,
# 将s2的索引增加到15个
# 如果新增加的索引值不存在,默认为 Nan
print(s2.reindex(index=range(15)))
'''
0 NaN
1 A
2 NaN
3 NaN
4 NaN
5 B
6 NaN
7 NaN
8 NaN
9 NaN
10 C
11 NaN
12 NaN
13 NaN
14 NaN
dtype: object
'''
# ffill : foreaward fill 向前填充,
# 如果新增加索引的值不存在,那么按照前一个非nan的值填充进去
print(s2.reindex(index=range(15), method='ffill'))
'''
0 NaN
1 A
2 A
3 A
4 A
5 B
6 B
7 B
8 B
9 B
10 C
11 C
12 C
13 C
14 C
dtype: object
'''
# reindex dataframe
df1 = DataFrame(np.random.rand(25).reshape([5, 5]), index=['A', 'B', 'D', 'E', 'F'], columns=['c1', 'c2', 'c3', 'c4', 'c5'])
print(df1)
'''
c1 c2 c3 c4 c5
A 0.700437 0.844187 0.676514 0.727858 0.951458
B 0.012703 0.413588 0.048813 0.099929 0.508066
D 0.200248 0.744154 0.192892 0.700845 0.293228
E 0.774479 0.005109 0.112858 0.110954 0.247668
F 0.023236 0.727321 0.340035 0.197503 0.909180
'''
# 为 dataframe 添加一个新的索引
# 可以看到 自动 扩充为 nan
print(df1.reindex(index=['A', 'B', 'C', 'D', 'E', 'F']))
''' 自动填充为 nan
c1 c2 c3 c4 c5
A 0.700437 0.844187 0.676514 0.727858 0.951458
B 0.012703 0.413588 0.048813 0.099929 0.508066
C NaN NaN NaN NaN NaN
D 0.200248 0.744154 0.192892 0.700845 0.293228
E 0.774479 0.005109 0.112858 0.110954 0.247668
F 0.023236 0.727321 0.340035 0.197503 0.909180
'''
# 扩充列, 也是一样的
print(df1.reindex(columns=['c1', 'c2', 'c3', 'c4', 'c5', 'c6']))
'''
c1 c2 c3 c4 c5 c6
A 0.700437 0.844187 0.676514 0.727858 0.951458 NaN
B 0.012703 0.413588 0.048813 0.099929 0.508066 NaN
D 0.200248 0.744154 0.192892 0.700845 0.293228 NaN
E 0.774479 0.005109 0.112858 0.110954 0.247668 NaN
F 0.023236 0.727321 0.340035 0.197503 0.909180 NaN
'''
# 减小 index
print(s1.reindex(['A', 'B']))
''' 相当于一个切割效果
A 1
B 2
dtype: int64
'''
print(df1.reindex(index=['A', 'B']))
''' 同样是一个切片的效果
c1 c2 c3 c4 c5
A 0.601977 0.619927 0.251234 0.305101 0.491200
B 0.244261 0.734863 0.569936 0.889996 0.017936
'''
# 对于一个 serie 来说,可以使用 drop,来丢掉某些 index
print(s1.drop('A'))
''' 就只剩下 三个了
B 2
C 3
D 4
dtype: int64
'''
# dataframe drop(A) 直接去掉一行
print(df1.drop('A', axis=0))
''' axis 默认 是 行
c1 c2 c3 c4 c5
B 0.571883 0.254364 0.530883 0.295224 0.352663
D 0.858452 0.379495 0.593284 0.786078 0.949718
E 0.556276 0.643187 0.808664 0.289422 0.501041
F 0.737993 0.286072 0.332714 0.873371 0.421615
'''
print(df1.drop('c1', axis=1))
''' 将 c1 的列 去掉
c2 c3 c4 c5
A 0.326681 0.247832 0.601982 0.145905
B 0.373961 0.393819 0.439284 0.926706
D 0.558490 0.617851 0.461280 0.373102
E 0.030434 0.566498 0.383103 0.739243
F 0.982220 0.989826 0.957863 0.411514
'''
pandas-18 reindex用法的更多相关文章
- Pandas之groupby( )用法笔记
groupby官方解释 DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True ...
- Pandas中Loc用法总结
摘自:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html 具体用法,假设数据源为: > ...
- pandas Timestamp的用法
(Timestamp('2018-08-01 00:00:00'), <class 'pandas._libs.tslibs.timestamps.Timestamp'>) 注意这里面的T ...
- Py修行路 Pandas 模块基本用法
pandas 安装方法:pip3 install pandas pandas是一个强大的Python数据分析的工具包,它是基于NumPy构建的模块. pandas的主要功能: 具备对其功能的数据结构D ...
- numpy和pandas和matplotlib用法
numpy result = [ [0, 10, 20, 30, 40], [10, 23, 33, 43, 53], [20, 83, 23, 55, 33], [30, 93, 44, 22, 5 ...
- python3 pandas DataFrame常见用法
df = pandas.read_clipboard() df 获取索引和值 df.index df.values DataFrame的values属性将数据以二维ndarray形式返回,dtype类 ...
- pandas的DataFrame用法
用来生成DataFrame数据 1.说明: class pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=F ...
- Pandas的基本用法
Pandas是使用python进行数据分析不可或缺的第三方库.我们已经知道,NumPy的ndarray数据结构能够很好地进行数组运算,但是当我们需要进行为数据添加标签,处理缺失值,对数据分组,创建透视 ...
- pandas.Series函数用法
class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) e.g., ...
随机推荐
- 【电脑】xshell报:需要Xmanager软件来处理X11转发请求
https://www.netsarang.com/zh/xmanager/ 下载了就好了 我的图片出不来,下了就好了.
- Scrapy笔记11- 模拟登录
Scrapy笔记11- 模拟登录 有时候爬取网站的时候需要登录,在Scrapy中可以通过模拟登录保存cookie后再去爬取相应的页面.这里我通过登录github然后爬取自己的issue列表来演示下整个 ...
- 记录Windows下文件操作记录
https://blog.csdn.net/huashuolin001/article/details/73863324
- Pandas | 19 合并/连接
Pandas具有功能全面的高性能内存中连接操作,与SQL等关系数据库非常相似.Pandas提供了一个单独的merge()函数,作为DataFrame对象之间所有标准数据库连接操作的入口 - pd.me ...
- STL——list用法总结
头文件 #include<list> 声明一个int型的list:list<int> a: 1.list的构造函数 list<int>a{1,2,3} list&l ...
- 【可持久化0/1Trie】【P4735】最大异或和
Description 给定一个长度为 \(n\) 的序列 \(A\),有 \(m\) 次操作,每次要么在序列尾部再添加一个数,将序列长度 \(n\) 加一,要么给进行一次查询,给定查询参数 \(l, ...
- tensorflow数据加载、模型训练及预测
数据集 DNN 依赖于大量的数据.可以收集或生成数据,也可以使用可用的标准数据集.TensorFlow 支持三种主要的读取数据的方法,可以在不同的数据集中使用:本教程中用来训练建立模型的一些数据集介绍 ...
- docker 创建私有镜像之 registry
一.下载 registry 镜像 [root@localhost ~]# docker pull registry Using default tag: latest latest: Pulling ...
- R程序书写规范
suppressMessages(library(xxx)) # 导入所需的R包 suppressMessages(library(xxx)) # 导入所需的R包 fucntion02 <- f ...
- 数据结构与算法系列——排序(4)_Shell希尔排序
1. 工作原理(定义) 希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本.但希尔排序是非稳定排序算法. 希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入 ...