Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何有条件地向数据帧添加一个热向量_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 如何有条件地向数据帧添加一个热向量

Python 如何有条件地向数据帧添加一个热向量,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有以下Python数据框架: import numpy as np import pandas as pd df = pd.DataFrame(np.array([[1, 2, 3], [3, 2, 1], [2, 1, 1]]), columns=['a', 'b', 'c']) df 输出时显示如下所示: a b c 0 1 2 3 1 3 2 1 2 2 1 1 我需要添加3个新列,如“d”列

我有以下Python数据框架:

import numpy as np
import pandas as pd
df  = pd.DataFrame(np.array([[1, 2, 3], [3, 2, 1], [2, 1, 1]]),
                   columns=['a', 'b', 'c'])
df
输出时显示如下所示:

    a   b   c
0   1   2   3
1   3   2   1
2   2   1   1
我需要添加3个新列,如“d”列、“e”列和“f”列。 每个新列中的值将根据“b”列和“c”列的值确定

在给定行中:

  • 如果列“b”的值大于列“c”的值,则列[d、e、f]将具有值[1、0、0]
  • 如果列“b”的值等于列“c”的值,则列[d、e、f]将具有值[0、1、0]
  • 如果列“b”的值小于列“c”的值,则列[d、e、f]将具有值[0、0、1]
完成此操作后,数据帧需要如下所示:

    a   b   c  d  e  f
0   1   2   3  0  0  1    # Since b smaller than c
1   3   2   1  1  0  0    # Since b bigger than c 
2   2   1   1  0  1  0    # Since b = c
我的原始数据帧比本例中的数据帧大得多。
在Python中有没有一种不循环数据帧的好方法呢?

您只需利用已经指定的布尔条件

df["d"] = np.where(df.b >  df.c, 1, 0)
df["e"] = np.where(df.b == df.c, 1, 0)
df["f"] = np.where(df.b <  df.c, 1, 0)
df[“d”]=np.where(df.b>df.c,1,0)
df[“e”]=np.where(df.b==df.c,1,0)
df[“f”]=np.where(df.b
您可以使用
np.where
创建条件向量,并使用
str.get\u dummies
创建假人

df['vec'] = np.where(df.b>df.c, 'd', np.where(df.b == df.c, 'e', 'f'))
df = df.assign(**df['vec'].str.get_dummies()).drop('vec',1)

    a   b   c   d   e   f
0   1   2   3   0   0   1
1   3   2   1   1   0   0
2   2   1   1   0   1   0

让我们试试
np。用
get\u dummies
进行签名,-1是cb


谢谢你的回答。您的解决方案出现错误:TypeError:无法将序列转换为。如何解决?如果我测试我的代码就可以解决,就像我应该做的那样。。。我的道歉。。。还有一个额外的
df
reference…新的解决方案也给出了“TypeError:无法将序列转换为”错误。我尝试了不同的方法,但也无法解决错误。对不起。。。我在工作中被打断了。。。这是一个局部编辑。np.sign的有趣用法!
df=df.join(np.sign(df.eval('c-b')).map({-1:'d',0:'e',1:'f'}).astype(str).str.get_dummies())
df
Out[29]: 
   a  b  c  d  e  f
0  1  2  3  0  0  1
1  3  2  1  1  0  0
2  2  1  1  0  1  0