【pandas】使用pipe对Series调用自定义函数的一个小例子

invocation 求助,调用,启用n.
palatable a.可接受的
it is far more palatable to write a single method per line;

# count the number of missing values 
fb_likes.isna().sum()

one potential downside of chaining is that debugging becomes difficult.
to debugging this code, I start by commenting out all of the commands except the first .注释掉除第一行外的其他行。
我也搞不懂我是在学英语还是在学编程语言……

.pipe()

# 这里自定义函数输出下面的chain methods中间结果到屏幕,可用于debug
def debug_ser(ser):
	print("before")
	print(ser)
	print("after")
	return ser
					
fb_likes.fillna(0).pipe(debug_ser).astype(int).head()
before
0        1000.0
1       40000.0
2       11000.0
3       27000.0
4         131.0
         ...   
4911      637.0
4912      841.0
4913        0.0
4914      946.0
4915       86.0
Name: actor_1_facebook_likes, Length: 4916, dtype: float64
after
Out[23]: 
0     1000
1    40000
2    11000
3    27000
4      131
Name: actor_1_facebook_likes, dtype: int32

# 这里自定义函数保存chain methods中间结果到一个全局变量中,也可以用于debug
# if you want to create a global variable to store an intermediate value 
# you can use .pipe
intermediate=None
def get_intermediate(ser):
    global intermediate
    intermediate=ser
    return ser
res=(fb_likes.fillna(0).pipe(get_intermediate).astype(int).head())
intermediate
Out[29]: 
0        1000.0
1       40000.0
2       11000.0
3       27000.0
4         131.0
         ...   
4911      637.0
4912      841.0
4913        0.0
4914      946.0
4915       86.0
Name: actor_1_facebook_likes, Length: 4916, dtype: float64
res
Out[30]: 
0     1000
1    40000
2    11000
3    27000
4      131
Name: actor_1_facebook_likes, dtype: int32

你可能感兴趣的:(python数据处理恩仇录,pandas,pipe)