pandas 运算

Data frame 和 series 的运算:
横列相加减:按照index ,row 的方向直接相加减。frame-series
纵列相加减:按照 columns,运用算术函数,相加减。
frame.sub(series3, axis=0)



'''operation between data frame and series '''
import numpy as np
from pandas import Series, DataFrame

arr = np.arange(12.).reshape((3, 4))
print(arr)
print("arr[0]:")
print(arr[0])
print("arr-arr[0]:")
print(arr-arr[0])

print("Operation between data frame and series")
frame = DataFrame(np.arange(12.).reshape((4, 3)), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon'])

series = frame.ix[0]

print("frame is \n", frame)
print("series is \n", series)
print("frame-series: \n", frame-series)

series2 = Series(range(3), index=['b', 'e', 'f'])
print("if an index value is not found in either the DataFrame's columns or the Series' index")
print("series2:\n", series2)
print("frame+series2:\n", frame+series2)


series3 = frame['d']
print("frame is \n", frame)
print("series3 is \n", series3)
print("if you want to instead broadcast over the columns,matching on the rows,you have to use one columns: ")
print("frame.sub(series3,axis=0: \n", frame.sub(series3, axis=0))

posted on 2017-05-14 18:11  小麦粒  阅读(507)  评论(0编辑  收藏  举报

导航