2017-09-19 203 views
2

我正在使用熊貓來排序通過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" 
+0

您有更具體的問題或錯誤嗎? – Legman

+0

請接受解決您的問題的答案 – JoYSword

回答

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 
+0

這工作...非常感謝! – dz333

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 
0

下面的代碼是一個簡單的方法來添加一列在大熊貓每個條件。

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