Pandas库03_Series和DataFrame数据结构_重索引

#这里学习索引
import pandas as pd

#下面来重建Series和DataFrame索引项

#Series类型的索引重建,是对索引重新排序reindex,没有的值就是NaN
# objs1=pd.Series([1,2,5,3,7,4,5],index=[1,2,3,4,5,6,7])
# print(objs1)
# objs2=objs1.reindex(["a","b","c","d","e","f","g"],fill_value="0") #不改原数据,缺省值fill_value
# print(objs2)

#DataFrame索引reindex
data={
"name":["唐浩","小王","老王","赵三","李四"],
"sex":["男","女","男","女","男"],
"year":[37,22,15,18,33],
"city":["成都","北京","上海","成都","深圳"]
}
df1=pd.DataFrame(data,index=["a","b","c","d","e"],columns=["name","year","sex","city"])
print(df1)
df2=df1.reindex(["a","c","d","e","b","f"],fill_value="00") #不改变原数据
print(df2)

猜你喜欢

转载自www.cnblogs.com/yiyea/p/11441793.html