目录
重建索引
import pandas as pd
import numpy
data = {
'语文': [0, 19, 9, 28, 91, 56],
'数学': [64, 81, 80, 17, 37, 52],
'英语': [40, 25, 45, 58, 67, 57],
'化学': [32, 38, 7, 12, 33, 28],
'物理': [69, 43, 44, 62, 3, 67],
'体育': [93, 7, 77, 95, 40, 89]
}
重建索引时,如果索引值不存在就会引入缺失值。
df = pd.DataFrame(data,index=['B', 'D', 'C', 'A', 'F', 'G'])
print(df)
print(df.reindex(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K']))
默认的缺失值为nan,可通过fill_value参数指定自定义的缺失值。
df = pd.DataFrame(data,index=['B', 'D', 'C', 'A', 'F', 'G'])
print(df)
print(df.reindex(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K'],fill_value='999'))
对于顺序数据(索引必须是单调递增或递减的),如时间序列或数值作为索引时,重建索引时会需要进行插值或填值处理,使用method参数进行设置。"backfill"和"bfill", 表示后项值填充;"ffill"和 "pad",表示前项值填充。
df = pd.DataFrame(data,index=[2, 6, 7, 9, 10, 11])
print(df)
print(df.reindex(numpy.arange(15),method='bfill'))
其他操作
将指定列设为索引
data = {
'语文': [0, 19, 9, 28, 91, 56],
'数学': [64, 81, 80, 17, 37, 52],
'英语': [40, 25, 45, 58, 67, 57],
'化学': [32, 38, 7, 12, 33, 28],
'物理': [69, 43, 44, 62, 3, 67],
'体育': [93, 7, 77, 95, 40, 89],
'科目': ['科一', '科二', '科三', '科四', '科五', '科六']
}
df = pd.DataFrame(data, index=[2, 6, 7, 9, 10, 'cs'])
print(df)
# 将指定列设为索引
print(df.set_index('科目'))
还原索引
还原索引后,将恢复系统默认的整型索引。
print(df.reset_index())