import numpy as np
import pandas as pd
from pandas import Series,DataFrame
Series reindex
s1 = Series([1,2,3,4],index=["A","B","C","D"])
s1
A 1
B 2
C 3
D 4
dtype: int64
s1.reindex(index=["A","B","C","D","E"])
A 1.0
B 2.0
C 3.0
D 4.0
E NaN
dtype: float64
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])
s2
1 a
5 b
10 c
dtype: object
s2.reindex(index=range(15))
0 NaN
1 a
2 NaN
3 NaN
4 NaN

本文介绍了Python数据科学中Pandas库的Reindex功能,讲解了如何对Series和DataFrame进行添加和删除索引的操作。通过示例展示了如何使用reindex方法调整数据结构,包括填充缺失值(NaN)以及简化数据框。
最低0.47元/天 解锁文章
443

被折叠的 条评论
为什么被折叠?



