[英]What is the `pandas` way to create a column in a dataframe by operating on each row?
I have an apply
function that operates on each row in my dataframe. 我有一个apply
函数,可以在我的数据帧中的每一行上运行。 The result of that apply
function is a new value. 该apply
函数的结果是一个新值。 This new value is intended to go in a new column for that row. 此新值旨在进入该行的新列。
So, after applying this function to all of the rows in the dataframe, there will be an entirely new column in that dataframe. 因此,在将此函数应用于数据框中的所有行之后,该数据框中将有一个全新的列。
How do I do this in pandas
? 我怎么在pandas
这样做?
Two ways primarily: 主要有两种方式:
df['new_column'] = df.apply(my_fxn, axis=1)
or 要么
df = df.assign(new_column=df.apply(my_fxn, axis=1))
If you need to use other arguments, you can pass them to the apply
function, but sometimes it's easier (for me) to just use a lambda: 如果你需要使用其他参数,你可以将它们传递给apply
函数,但有时候(对我来说)使用lambda更容易:
df['new_column'] = df.apply(lambda row: my_fxn(row, global_dict), axis=1)
Additionally, if your function can operate on arrays in a vectorized fashion, you could just do: 此外,如果您的函数可以以矢量化方式在数组上运行,您可以这样做:
df['new_column'] = my_fxn(df['col1'], df['col2'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.