简体   繁体   English

如何从 zip function 简洁地创建新列作为 output?

[英]How to concisely create new columns as output from a zip function?

I have a dataframe I am adding new columns to.我有一个 dataframe 我正在添加新列。 I am doing this using output from zip that uses output from a function.我正在使用 zip 的 output 执行此操作,该 output 使用来自zip的 output Seen below:见下图:

The function generates 4 new columns that I am trying to add to an existing dataframe. function 生成 4 个新列,我试图将它们添加到现有 dataframe 中。

data = [
    [1, 123],
    [2, 123454],
    [3, 64564],
]

df = pd.DataFrame(data, columns=["ID", "number"])

# function
def func(num):
    double = num * 2
    triple = num * 3
    quadruple = num * 4
    tenex = num * 10

    return double, triple, quadruple, tenex

# apply function to create 4 new columns
df["double"], df["triple"], df["quad"], df["tenex"] = zip(
    *df["number"].apply(lambda x: func(x))
)

Is there a more concise way to do this?有没有更简洁的方法来做到这一点? It's fine when I am adding only 4 columns, but I want to expand this function to add 10+ columns.当我只添加 4 列时很好,但我想扩展这个 function 以添加 10+ 列。

I was considering something like this:我正在考虑这样的事情:

tuple(df[colname] for colname in col_list) = zip(
    *df["number"].apply(lambda x: func(x))
)

but it doesn't work (error: SyntaxError: cannot assign function to call )但它不起作用(错误: SyntaxError: cannot assign function to call

I'd use a dictionary to map column names to functions - you don't really benefit from having all the computations done inside a single function.我会使用字典将 map 列名用于函数 - 在单个 function 内完成所有计算并没有真正受益。

something like:就像是:

column_mapper = {
    'double': lambda x: x*2,
    'triple': lambda x: x*3,
    'quadruple': lambda x: x*4,
}


data = [
    [1, 123],
    [2, 123454],
    [3, 64564],
]
df = pd.DataFrame(data, columns=["ID", "number"])

for column_name, func in column_mapper.items():
    df[column_name] = df['number'].apply(func)

Pass result_type='expand' to apply to output to multiple columns: result_type='expand'传递给apply到多个列:

df[["double","triple","quad","tenex"]] = df.apply(lambda x: func(x['number']), axis = 1, result_type='expand')

I think the better way would actually be to create separate functions here.我认为更好的方法实际上是在这里创建单独的函数。 Create one function that takes two arguments, x and n , and then use functools.partial to create single-argument functions to use with apply :创建一个 function ,它需要两个 arguments、 xn ,然后使用functools.partial创建与apply一起使用的单参数函数:

from functools import partial


def multiply(x, n):
    return x * n


functions = ((col, partial(multiply, n=i)) for col, i in [('double', 2), ('triple', 3), ('quadruple', 4), ('tenx', 10)])

for col, func in functions:
    df[col] = df['number'].apply(func)

enumerate could work as long as the cols list in in the same order as tuple returned in func.只要 cols 列表的顺序与 func 中返回的元组的顺序相同,enumerate 就可以工作。

cols = ['double', 'triple', 'quad', 'tenex']

for i, col in enumerate(cols):
    df[col] = df["number"].apply(lambda x: func(x)[i])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM