根据其他列的条件生成多个列 [英] Generate multiple columns based on conditions from other columns

查看:77
本文介绍了根据其他列的条件生成多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多解决方案,但是几乎所有问题都与创建单个列有关。
所以,这是我的问题。

I've searched quite a bit for a solution, but almost all questions are related to the creation of a single column. So, here is my problem.

给出一个示例DataFrame:

Given an example DataFrame:

df = pd.DataFrame({
    "blue": [5, 5, 4], 
    "red": [1, 7, 5],
    "yellow": [3, 9, 0],
    "orange": [9, 7, 3],
    "config": ["north", "south", "north"]
})



   blue config  orange  red  yellow
0     5  north       9    1       3
1     5  south       7    7       9
2     4  north       3    5       0

我想要实现的是基于多个条件(具体来说是映射)创建其他列。这是我尝试过的一个示例:

What I would like to achieve is to create additional columns based on multiple conditions (a mapping to be specific). Here is an example of what I have tried:

def gen_col(row):

    if row["config"] == "north":
        new_blue = row["blue"]
        new_red = row["red"]
        new_yellow = row["yellow"]
        new_orange = row["orange"]
        return new_blue, new_red, new_yellow, new_orange
    elif row["config"] == "south":
        new_blue = row["orange"]
        new_red = row["yellow"]
        new_yellow = row["red"]
        new_orange = row["blue"]
        return new_blue, new_red, new_yellow, new_orange

df["new_blue", "new_red", "new_yellow", "new_orange"] = df.apply(gen_col, axis=1)

但是,这将返回以下内容:

However, this returns the following:

   blue config  orange  red  yellow (new_blue, new_red, new_yellow, new_orange)
0     5  north       9    1       3             (5, 1, 3, 9)
1     5  south       7    7       9             (7, 9, 7, 5)
2     4  north       3    5       0             (4, 5, 0, 3)                         

关于如何创建分隔新列?

推荐答案

使用 result_type ='expand'< <$中的/ code>参数c $ c> DataFrame.apply ,还为分配的列添加嵌套列表:

Use result_type='expand' parameter in DataFrame.apply and also add nested lists for assigned columns:

df[["new_blue", "new_red", "new_yellow", "new_orange"]] = df.apply(gen_col, axis=1, result_type='expand')
print (df)
   blue  red  yellow  orange config  new_blue  new_red  new_yellow  new_orange
0     5    1       3       9  north         5        1           3           9
1     5    7       9       7  south         7        9           7           5
2     4    5       0       3  north         4        5           0           3

这篇关于根据其他列的条件生成多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆