# -*- coding: utf-8 -*-
"""
Created on Thu May 23 15:50:35 2019
@author: User
"""
import pandas as pd
import numpy as np
cs1=pd.Series([1.5,2.5,3,5,1],index=['a','c','d','b','e'])
print("\n cs1:")
print(cs1)
cs2=pd.Series([10,20,30,50,10,100,20],index=['c','a','e','b','f','g','d'])
print("\n cs2:")
print(cs2)
cdf1=pd.DataFrame(np.arange(10).reshape((2,5)),columns=list('bcaed'))
print("\n cdf1:")
print(cdf1)
cdf2=pd.DataFrame(np.arange(12).reshape((3,4)),columns=list('abcd'))
print("\n cdf2:")
print(cdf2)
print("\n reversef=lambda x: -x:")
reversef=lambda x: -x
print(reversef(cs2))
print("\n reversef=lambda x: x.max()-x.min():")
rangef=lambda x: x.max()-x.min()
print(rangef(cs2))
print("\n rangef(cdf1.add(cdf2,fill_value=0)):")
print(rangef(cdf1.add(cdf2,fill_value=0)))
print("\n (cdf1.add(cdf2,fill_value=0)).apply(rangef,axis=0):")
print((cdf1.add(cdf2,fill_value=0)).apply(rangef,axis=0))
print("\n (cdf1.add(cdf2,fill_value=0)).apply(rangef,axis=1):")
print((cdf1.add(cdf2,fill_value=0)).apply(rangef,axis=1))
运行:
cs1:
a 1.5
c 2.5
d 3.0
b 5.0
e 1.0
dtype: float64
cs2:
c 10
a 20
e 30
b 50
f 10
g 100
d 20
dtype: int64
cdf1:
b c a e d
0 0 1 2 3 4
1 5 6 7 8 9
cdf2:
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
reversef=lambda x: -x:
c -10
a -20
e -30
b -50
f -10
g -100
d -20
dtype: int64
reversef=lambda x: x.max()-x.min():
90
rangef(cdf1.add(cdf2,fill_value=0)):
a 9.0
b 9.0
c 9.0
d 9.0
e 5.0
dtype: float64
(cdf1.add(cdf2,fill_value=0)).apply(rangef,axis=0):
a 9.0
b 9.0
c 9.0
d 9.0
e 5.0
dtype: float64
(cdf1.add(cdf2,fill_value=0)).apply(rangef,axis=1):
0 6.0
1 8.0
2 3.0
dtype: float64