pandas在apply中加入参数

#!/usr/bin/python

import pandas as pd

data = {'year':[2000,2001,2002,2001,2002],'value':[1.5,1.7,3.6,2.4,2.9]}

frame = pd.DataFrame(data)
print (frame)
print ("---------------")
def testfunc(x, data):  #第一个参数代表该函数处理的每一个元素,第二个参数args是传入的参数
    return str(x) + "__" + str(data)

for col in frame.columns:
    frame[col] = frame[col].apply(testfunc, args = (col,)) #('ok',)表示一个参数
print ('=============')
print (frame)


#输出
#   year  value
# 0  2000    1.5
# 1  2001    1.7
# 2  2002    3.6
# 3  2001    2.4
# 4  2002    2.9
# ---------------
# =============
#          year       value
# 0  2000__year  1.5__value
# 1  2001__year  1.7__value
# 2  2002__year  3.6__value
# 3  2001__year  2.4__value
# 4  2002__year  2.9__value

 

上一篇:酷炫!36张图爱上高级可视化库Plotly_Express!


下一篇:华为HCIE R&S笔记-22OSPF报文类型