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

Posted yiyea

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

以上是关于Pandas库03_Series和DataFrame数据结构_重索引的主要内容,如果未能解决你的问题,请参考以下文章

pandas一些基本操作(DataFram和Series)_1

pandas一些基本操作(DataFram和Series)_2

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

Python Pandas 数据框创建

Python Pandas 数据框创建

Pandas库01_Series数据类型