码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-09-01 12:49:09      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:series   val   data   改变   panda   dataframe   value   dex   老王   

#这里学习索引
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)

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

标签:series   val   data   改变   panda   dataframe   value   dex   老王   

原文地址:https://www.cnblogs.com/yiyea/p/11441793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!