简体   繁体   English

在python pandas中根据if-elif-else条件创建一个新列

[英]Creating a new column based on if-elif-else condition in python pandas

I want to add a d column to a DataFrame.我想向 DataFrame 添加一个d列。 When creating the d column, the conditions are as follows:创建d列时,条件如下:

If it starts with 11 in column a , it gets the value in column b , otherwise it gets the value in column c .如果它在a列中以 11 开头,则获取b列中的值,否则获取c列中的值。

a     b    c
11101 100  150
11201 200  250
13301 300  350


a     b    c    d
11101 100  150  100
11201 200  250  200
13301 300  350  350

Use np.where after converting the column a into str :将列a转换为str后使用np.where

df["a"] = df["a"].astype("str")
df["d"] = np.where(df["a"].str.startswith("11"), df["b"], df["c"])

or if you don't want to change datatype of a in original dataframe或者如果您不想更改原始数据帧中a的数据类型

df["d"] = np.where(df["a"].astype("str").str.startswith("11"), df["b"], df["c"])

您还可以使用 lambda 函数:

df["d"] = df.apply(lambda x : x["b"] if str(x["a"])[:2] == "11" else x["c"], axis =1)
df["d"]=0 for(i , x in enumerate(df["a"])): if str(x).startswith("11"): df["d"][i]=df["b"][i] else: df["d"][i]=df["c"][i] print(df)

To keep it readable, I would prefer the use of an external function ( calculateValue ) in the lambda function为了保持可读性,我更喜欢在 lambda 函数中使用外部函数( calculateValue

def calculateValue(a, b, c):
  if(a.startswith('11')):
    return b
  else:
    return c

df["d"] = df.apply(lambda row: calculateValue(row["a"],row["b"],row["c"]), axis=1)

You can also use numpy.vectorize which can be a bit faster.您还可以使用numpy.vectorize ,它可以更快一些。

import numpy as np


def get_d(a, b, c):
    return b if str(a)[:2] == '11' else c


df['d'] = np.vectorize(get_d)(df.a, df.b, df.c)

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

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