学习pandas中一些常用的功能
1.
Series的reindex()方法能修改Series的index,如果修改的index与创建的index没有对应的话,就会引入缺失值
import pandas as pd
import numpy as np
obj=pd.Series([4.1,4.2,5.7,8.9],index=['a','b','c','d'])
print(obj)
obj1=obj.reindex(['a','b','e'])
print(obj1)
reindex()方法也可以修改DataFrame的columns index 或则 rows index ,和Series一样没有对应的,将为缺失值。注意在修改DataFrame的columns,要在reindex()注明frame.reindex(columns)
frame=pd.DataFrame(np.arange(9.).reshape(3,3),index=['a','b','c'],columns=['one','two','three'])
print(frame)
frame1=frame.reindex(['a','b','c','d'])
print(frame1)
state=['one','two','four']
frame2=frame.reindex(columns=state)
print(frame2)
也可以使用更简洁的loc()来该改变index
frame3=frame.loc[['a','b','c','d'],state]
print(frame3)
2.按轴删除记录(axis)
drop()方法会删去传入的参数(axis)对应的某一行
对于Series,删除后悔返回一个新的obj。