2016-12-14 52 views
1
import pandas as pd 

df = pd.DataFrame([['Dog', 10, 6], ['Cat', 7 ,5]], columns=('Name','Amount','Day')) 

Name Amount Day 
Dog 10 6 
Cat 7  5 

我想作数据框如下所示:如果打出>值,除,增值列 - 的Python /大熊猫

Name Amount Day 
Dog1 6  6 
Dog2 2.5 7 
Dog3 1.5 8 
Cat 7  5 

First step: For any Amount > 8, split into 3 different rows, with new name of 'Name1', 'Name2','Name3' 
Second step: 
For Dog1, 60% of Amount, Day = Day. 
For Dog2, 25% of Amount, Day = Day + 1. 
For Dog3, 15% of Amount, Day = Day + 2. 

Keep Cat the same because Cat Amount < 8 

任何想法?任何帮助,将不胜感激。

回答

1
df = pd.DataFrame([['Dog', 10, 6], ['Cat', 7 ,5]], columns=('Name','Amount','Day')) 

template = pd.DataFrame([ 
     ['1', .6, 0], 
     ['2', .25, 1], 
     ['3', .15, 2] 
    ], columns=df.columns) 

def apply_template(r, t): 
    t = t.copy() 
    t['Name'] = t['Name'].radd(r['Name']) 
    t['Amount'] *= r['Amount'] 
    t['Day'] += r['Day'] 
    return t 

pd.concat([apply_template(r, template) for _, r in df.query('Amount > 8').iterrows()], 
      ignore_index=True).append(df.query('Amount <= 8'), ignore_index=True) 

enter image description here

+0

这个工作。非常感谢! – sschade