Python 如何有条件地向数据帧添加一个热向量
我有以下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”列
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