pandas运算、排序、排名

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/castingA3T/article/details/78892434
import pandas as pd
import numpy as np
#Series运算
s1=pd.Series([7.3,-2.5,3.4,1.5],index=['a','c','d','e'])
s2=pd.Series([-2.1,3.6,-1.5,4,3.1],index=['a','c','e','f','g'])
print(s1+s2)
print(s1.add(s2,fill_value=0))#加法
#DataFrame运算
df1=pd.DataFrame(np.arange(9).reshape((3,3,)),columns=list('bcd'),index=['aa','bb','cc'])
df2=pd.DataFrame(np.arange(12).reshape((4,3)),columns=list('bde'),index=['aa','dd','bb','ee'])
print(df1+df2)
print(df1.sub(df2))#减法
#广播
arr=np.arange(12).reshape((3,4))
print(arr)
print(arr-arr[0])
#按列方向传播
frame=pd.DataFrame(np.arange(9).reshape(3,3),index=['aa','bb','cc'],columns=['a','b','c'])
print(frame)
series1=frame['b']
print(series1)
print(frame.sub(series1,axis=0))
print('======================================')
#Series索引排序
obj=pd.Series(range(4),index=['d','a','b','c'])
print(obj)
print(obj.sort_index())
#DataFrame索引排序
frame=pd.DataFrame(np.arange(8).reshape((2,4)),index=['three','one'],columns=['d','a','b','c'])
print(frame)
print(frame.sort_index(axis=1))#按columns排序
print(frame.sort_index(axis=1,ascending=False))#降序
obj=pd.Series([4,7,-3,2])
#obj.order()
frame=pd.DataFrame({'b':[4,7,-3,2],'a':[0,1,0,1]})
print(frame)
print(frame.sort_values(by='b'))#按某列数值排序
#排名
obj=pd.Series([7,-5,7,4,2,0,4])
print(obj.rank())
print(obj.rank(method='first'))
print(obj.rank(ascending=False,method='min'))
frame=pd.DataFrame({'b':[4,7,-3,2],'a':[0,1,0,1],'c':[-2,5,8,-2]})
print(frame)
print(frame.rank(axis=1))#按行排名
展开阅读全文

没有更多推荐了,返回首页