示例数据帧
category Nestle Britania P&G HL
lead 0 1 0 0
Mg 1 0 0 0
Cl 0 0 1 0
首先,我们必须确保每一行都没有一个以上的1。
第二,所需的输出是数据帧
category company_name
lead britania
Mg Nestle
Cl P&G
我试着像下面这样
df['company_name'] = df.apply(lambda row: valuation_formula(row), axis=1)
def valuation_formula(row):
return sent the column name which has result 1
你能帮帮忙吗
发布于 2018-07-13 04:08:30
使用dot
df['New']=df.iloc[:,1:].dot(df.columns[1:])
df
Out[106]:
category Nestle Britania P&G HL New
0 lead 0 1 0 0 Britania
1 Mg 1 0 0 0 Nestle
2 Cl 0 0 1 0 P&G
https://stackoverflow.com/questions/51313634
复制相似问题