如何在将列加在一起时避免创建太多变量?我有一些需要满足的条件,并且每个新语句都会在不满足条件的地方洗掉旧信息。那么,如何保留旧值并添加新值?

采取这个DataFrame

import pandas as pd
import datetime as DT

d = {'case' : pd.Series([1,1,1,1,2]),
  'open' : pd.Series([DT.datetime(2014, 3, 2), DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2),DT.datetime(2014, 3, 2)]),
'change' : pd.Series([DT.datetime(2014, 3, 8), DT.datetime(2014, 4, 8),DT.datetime(2014, 5, 8),DT.datetime(2014, 6, 8),DT.datetime(2014, 6, 8)]),
'StartEvent' : pd.Series(['Homeless','Homeless','Homeless','Homeless','Jail']),
'ChangeEvent' : pd.Series(['Homeless','Jail','Homeless','Jail','Jail']),
'close' : pd.Series([DT.datetime(2015, 3, 2), DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2),DT.datetime(2015, 3, 2)])}
df=pd.DataFrame(d)


这给了我我需要的部分信息。

df['homeless']=(df.groupby('case')['change'].apply(lambda x: x - x.shift(1) )[(df.ChangeEvent.shift(1)=='Homeless')])/np.timedelta64(1,'D')
df['jail']=(df.groupby('case')['change'].apply(lambda x: x- x.shift(1) )[(df.ChangeEvent.shift(1)=='Jail')])/np.timedelta64(1,'D')
df.homeless=df.homeless.fillna(0)
df.jail=df.jail.fillna(0)


df.loc[df.groupby(['case']).apply(lambda x: x['change'].idxmin()), 'first']=1
df.loc[df.groupby(['case']).apply(lambda x: x['change'].idxmax()), 'last']=1


理想情况下,我可以参加下一部分并将其放入相同的变量“无家可归”,“监狱”中,但是无论我尝试执行什么操作,都将删除不满足条件的当前变量

df['homeless2']=(df['homeless']+(df['change']-df['open'])/np.timedelta64(1,'D'))[(df['ChangeEvent']=='Homeless') & (df['first']==1)]


例如,下一行将在不满足条件的地方移出。我如何保留旧值并添加新值。

#df['homeless2']=(df['homeless']+(df['change']-df['open'])/np.timedelta64(1,'D'))[(df['ChangeEvent']=='Homeless') & (df['first']==1)]

df['jail2']=(df['jail']+(df['change']-df['open'])/np.timedelta64(1,'D'))[(df['ChangeEvent']=='Jail') & (df['first']==1)]
df.homeless2=df.homeless2.fillna(0)
df.jail2=df.jail2.fillna(0)

df['homeless3']=(df['homeless']+(df['close']-df['change'])/np.timedelta64(1,'D'))[(df['ChangeEvent']=='Homeless') & (df['last']==1)]
df['jail3']=(df['jail']+(df['close']-df['change'])/np.timedelta64(1,'D'))[(df['ChangeEvent']=='Jail') & (df['last']==1)]
df.homeless3=df.homeless3.fillna(0)
df.jail3=df.jail3.fillna(0)

df['realjail']=df.jail+df.jail2+df.jail3
df['realhomeless']=df.homeless+df.homeless2+df.homeless3


这行得通,但远非有效。谢谢。

最佳答案

您正在做的第一部分;稍微清理

In [51]: df=pd.DataFrame(d)

In [52]: changes = df.groupby('case')['change']

In [53]: df['jail'] = (changes.diff()[df.ChangeEvent.shift(1)=='Jail']/np.timedelta64(1,'D'))

In [54]: df['homeless'] = (changes.diff()[df.ChangeEvent.shift(1)=='Homeless']/np.timedelta64(1,'D'))

In [55]: df['homeless'].fillna(0,inplace=True)

In [56]: df['jail'].fillna(0,inplace=True)

In [57]: df.loc[changes.idxmax(), 'last']=1

In [58]: df.loc[changes.idxmin(), 'first']=1

In [59]: df
Out[59]:
  ChangeEvent StartEvent  case     change      close       open  jail  homeless  last  first
0    Homeless   Homeless     1 2014-03-08 2015-03-02 2014-03-02     0         0   NaN      1
1        Jail   Homeless     1 2014-04-08 2015-03-02 2014-03-02     0        31   NaN    NaN
2    Homeless   Homeless     1 2014-05-08 2015-03-02 2014-03-02    30         0   NaN    NaN
3        Jail   Homeless     1 2014-06-08 2015-03-02 2014-03-02     0        31     1    NaN
4        Jail       Jail     2 2014-06-08 2015-03-02 2014-03-02     0         0     1      1

[5 rows x 10 columns]


您不必创建这是新列,但恕我直言有点干净

In [62]: df['homeless_change'] = df['homeless']+(df['change']-df['open'])/np.timedelta64(1,'D')


这是告诉loc设置哪些行的关键

In [63]: homeless_mask = (df['ChangeEvent']=='Homeless') & (df['first']==1)


仅对行掩码和您指定的列进行对齐

In [64]: df.loc[homeless_mask,'homeless'] = df['homeless_change']

In [65]: df
Out[65]:
  ChangeEvent StartEvent  case     change      close       open  jail  homeless  last  first  homeless_change
0    Homeless   Homeless     1 2014-03-08 2015-03-02 2014-03-02     0         6   NaN      1                6
1        Jail   Homeless     1 2014-04-08 2015-03-02 2014-03-02     0        31   NaN    NaN               68
2    Homeless   Homeless     1 2014-05-08 2015-03-02 2014-03-02    30         0   NaN    NaN               67
3        Jail   Homeless     1 2014-06-08 2015-03-02 2014-03-02     0        31     1    NaN              129
4        Jail       Jail     2 2014-06-08 2015-03-02 2014-03-02     0         0     1      1               98

[5 rows x 11 columns]

关于python - 在满足条件的地方添加列,但在pandas python中保留先前的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22086613/

10-09 08:35