[英]Apply series of transformations to pandas DataFrame object
I am relatively new to Python programming. 我对Python编程比较陌生。 I have a pandas DataFrame object say obj1.
我有一个熊猫DataFrame对象,说obj1。 I need to apply a series of transformation to records stored in obj1['key'].
我需要对存储在obj1 ['key']中的记录进行一系列转换。 Suppose obj1['key'] has 300 entries and I need to apply func1 then func2 then func3 on each of the 300 entries and store the final result in obj1['key'].
假设obj1 ['key']有300个条目,我需要在300个条目中的每一个上应用func1,然后分别应用func2和func3,并将最终结果存储在obj1 ['key']中。 One way would be to do as below.
一种方法是执行以下操作。 Is there a better way to do the same?
有没有更好的方法可以做到这一点?
obj1['key']=[func3(func2(func1(item))) for item in obj1['key']]
Python generators can't be used for this purpose right. Python生成器不能用于此目的。
是的,您可以使用默认方法DataFrame.apply()
df = df.apply(f1).apply(f2).apply(fn)
define a function 定义一个功能
def recursivator(x, fs):
return recursivator(fs[-1](x), fs[:-1]) if len(fs) > 0 else x
x
is the thing being operated on, fs
is a list of functions. x
是要操作的东西, fs
是功能列表。
df = df.applymap(lambda x: recursivator(x, fs))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.