pandas的常用函数

1.DataFrame的常用函数: 
(1)np.abs(frame) 绝对值,
(2)apply function,
  lambda f= lambda x: x.max()-x.min(),frame.apply(f); frame.apply(f,axis = 1)
  f(x), def f(x):
      return Series([x.min(),x.max()], index=['min','max']),frame.apply(f)
(3) applymap format
  f= lambda x:'%.2f' %x, frame.applymap(f) 或者 frame['e'].map(format)
2. index 或者 column的排序
行排序:frame.sort_index()
列排序:frame.sort_index(axis=1)
列降序排列:frame.sort_index(axis=1,ascending=False)
通过值进行排序:
Series.sort_values()
frame.sort_values(by = 'b')
frame.sort_values(by = ['a','b'])
3.
排名(Series.rank(method='average', ascending=True))的作用与排序的不同之处在于,
他会把对象的 values 替换成名次(从 1 到 n)。这时唯一的问题在于如何处理平级项,方法里的 method 参数就是起这个作用的,
他有四个值可选:average, min, max, first
Series.rank()
frame.rank(axis=1) 按照columns 进行排序。
4.


'''function application and mapping'''
import numpy as np
from pandas import DataFrame , Series
frame = DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])
print("frame is \n", frame)
print("np.abs(frame) is \n", np.abs(frame))
print("another frequent operation is applying a function on 1D arrays to each column or row.\n DataFrame's apply method does exactly this:")
f = lambda x: x.max()-x.min()
print("f = lambda x: x.max()-x.min()")
print("frame.apply(f):", frame.apply(f))
print("frame.apply(f,axis=1):",frame.apply(f,axis=1))
def f(x):
return Series([x.min(), x.max()], index=['min', 'max'])
print("frame.apply(f): \n", frame.apply(f))
print("the function pass to apply need not to return a scalar value,it can also return a series with multiple values")

format = lambda x: '%.2f' % x
print("frame.applymap(format): \n", frame.applymap(format))
print("frame['e'].map(format): \n", frame['e'].map(format))


obj = Series(range(4),index=['d', 'a', 'b', 'c'])
print("obj.sort_index: \n", obj.sort_index())

frame = DataFrame(np.arange(8).reshape((2, 4)), index=['three', 'one'], columns= ['d', 'a', 'b', 'c'])
print("frame is \n", frame)
print("frame.sort_index() \n", frame.sort_index())
print("frame.sort_index(axis=1) \n", frame.sort_index(axis=1))

print("frame.sort_index(axis=1,ascending=False): \n", frame.sort_index(axis=1,ascending=False))

obj= Series([4, 7, -3, 2])
print("obj: \n", obj)
print("obj.sort_values(): \n", obj.sort_values())

obj1 = Series([4, np.nan, 7, np.nan, -3, 2])
print("obj1:",obj1)
print("obj1.sort_values():\n", obj1.sort_values())

frame1 = DataFrame({'b':[4,7,-3,2],'a':[0,1,0,1]})
print("frame1 is \n",frame1)
print("frame1.sort_values(by='b')\n",frame1.sort_values(by='b'))
print("frame1.sort_values(by=['a','b'] \n", frame1.sort_values(by=['a','b']))

print("Ranking is closely related to sorting,assigning ranks from one through the number of valid data points in an array")
obj2 = Series([7, -5, 7, 4, 2, 0, 4])
print("obj2.rank() is \n", obj2.rank())

obj2 = Series([7, -5, 7, 4, 2, 0, 4])
print("obj2.rank() is \n", obj2.rank())
print("obj2.rank(method='min') \n",obj2.rank(method='min'))
print("obj2.rank(method='max') \n",obj2.rank(method = 'max'))
print("obj2.rank(method='first' \n",obj2.rank(method = 'first'))
print("obj2.rank(method='dense' \n", obj2.rank(method = 'dense'))

frame2 = DataFrame({'b':[4.3, 7, -3,2],'a':[0,1,0,1],'c':[-2,5,8,-2.5]})
print("frame2 is \n",frame2)
print("frame2.rank(axis=1) \n",frame2.rank(axis=1))


posted on 2017-05-15 23:03  小麦粒  阅读(2768)  评论(0编辑  收藏  举报

导航