pandas条件语句和添加列

时间:2017-09-19 20:59:37

标签: python pandas if-statement dataframe

我正在使用Pandas对excel电子表格进行排序。我想执行if / elif函数在我的数据帧中的新列中返回一个字符串。我正在尝试返回名为" action"的新列。返回基于时间值的字符串。

state     time      
 ca         1
 ca         5
 ca         7
 ca         10

for rows in df:

 if df[time]>=1:
    return "do nothing"

 elif df[time]<=5:
     return "add more"

 elif df[time]<=10:
      return "add less"

  else:
      return "error"

3 个答案:

答案 0 :(得分:4)

IIUC我们可以使用pd.cut()方法:

In [167]: df['new'] = pd.cut(df.time, 
                             bins=[-np.inf, 1, 5, 10, np.inf], 
                             labels=['do nothing','add more','add less','error'])

In [168]: df
Out[168]:
  state  time         new
0    ca     1  do nothing
1    ca     5    add more
2    ca     7    add less
3    ca    10    add less

答案 1 :(得分:2)

使用np.searchsorted

labels = np.array(['do nothing', 'add more', 'add less', 'error'])
df.assign(new=labels[np.searchsorted([1, 5, 10], df.time.values)])

  state  time         new
0    ca     1  do nothing
1    ca     5    add more
2    ca     7    add less
3    ca    10    add less

答案 2 :(得分:0)

以下代码是为pandas中的每个条件添加列的简单方法。

import pandas as pd
from io import StringIO

csv = StringIO("""state,time
ca,1
ca,5
ca,7
ca,10""")
df = pd.read_csv(csv)
# Out[1]:
#   state  time
# 0    ca           1
# 1    ca           5
# 2    ca           7
# 3    ca          10

def add_action(row):
    if row["time"] <= 1:
        return "do nothing"
    elif row["time"] <= 5:
        return "add more"
    elif row["time"] <= 10:
        return "add less"
    else:
        return "error"

df = df.assign(action=df.apply(add_action, axis=1))
# Out[2]: 
#   state  time      action
# 0    ca     1  do nothing
# 1    ca     5    add more
# 2    ca     7    add less
# 3    ca    10    add less