在熊猫中创建常量列

时间:2018-10-28 15:52:06

标签: python pandas dataframe

伙计们,您知道我应该如何在数据框中为每个帐户的第一个观察值创建一个列:

Account    | Month      | Debt_left 
Acc1       |     0      |     -100
Acc1       |     1      |     -90
Acc2       |     0      |     -120
Acc2       |     1      |     -110

To become:
Account    | Month      | Debt_left | initial_debt
Acc1       |     0      |     -100  |    -100
Acc1       |     1      |     -90   |    -100
Acc2       |     0      |     -120  |    -120
Acc2       |     1      |     -110  |    -120

2 个答案:

答案 0 :(得分:1)

您之后需要重命名列,但这可以解决问题:

import pandas as pd

df = pd.DataFrame( {"Account" : ["Acc1","Acc1","Acc2","Acc2"],
               "Month" : [0,1,0,1],
               "Debt_left" : [-100,-90,-120,-110]})

pd.merge( df, df.groupby('Account').first(), how = "inner", on="Account"  )[["Account","Month_x","Debt_left_x","Debt_left_y"]] 

答案 1 :(得分:0)

# Assuming that the debt_left does not increase

import io
import pandas as pd

data = io.StringIO('''
Account    | Month      | Debt_left 
Acc1       |     0      |     -100
Acc1       |     1      |     -90
Acc2       |     0      |     -120
Acc2       |     1      |     -110

''')

df = pd.read_csv(data, sep='|')
df.columns = df.columns.str.strip()

mapper = df.set_index('Month').groupby('Account')['Debt_left'].min().to_dict()

df['init_value'] = df['Account'].map(mapper)

print(df)