1、重新索引
2、丢弃指定轴上的项
附源码:
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
# In[2]:
obj = Series([4.5,2.3,-7,6],index= [3,4,1,2])
print (obj)
# In[3]:
# 调用Srries的reindex进行重排
obj2 = obj.reindex([1,2,3,4,5])
print(obj2)
# In[4]:
obj2 = obj.reindex([1,2,3,4,5,6],fill_value=0) # fill_value是把NaN值进行赋值
print(obj2)
# In[5]:
obj3 = Series(['blue','purple','yellow'],index=[0,2,4])
print( obj3)
# In[6]:
re_obj3 = obj3.reindex(range(6),method='ffill') # ffill向前充值
print(re_obj3)
# In[7]:
frame = DataFrame(np.arange(9).reshape((3,3)),index=['a','c','d'],
columns=['Guangdong','Shandong','Henan'])
print (frame)
# In[8]:
frame2 = frame.reindex(['a','b','c','d'],method='ffill')
print (frame2)
# In[9]:
# 使用columns关键字索引列
province = ['Sichuan','Henan','Anhui']
frame2 = frame.reindex(columns=province)
print (frame2)
# In[10]:
# 同时对行和列索引
frame2 = frame.reindex(index=['a','b','c','d'],columns=province)
print (frame2)
# In[11]:
# 利用ix标签索引功能
frame3 = frame.ix[['a','b','c','d'],province]
print(frame3)
# In[12]:
obj = Series(np.arange(5),index=['a','b','c','d','e'])
print(obj)
# In[13]:
# 丢弃指定轴上的项
new_obj = obj.drop('c')
print(new_obj)
# In[14]:
# 使用DataFrame删除任意轴上的索引值
data = DataFrame(np.arange(16).reshape((4,4)),
index=['Guangdong','Shandong','Sichuan','Henan'],
columns=['one','two','three','four'])
print(data)
# In[15]:
data2 = data.drop(['Sichuan','Shandong'])
print(data2)
# In[16]:
data2 = data.drop('two',axis=1)
print(data2)
# In[17]:
data2 = data.drop(['two','four'],axis=1)
print(data2)
网友评论