我想明智地交换行条件

时间:2019-12-27 14:26:18

标签: python pandas dataframe analysis

我们有4列贷方,借方,旁白和余额。在贷方列中,金额将贷记到余额列中 借方列金额将从余额列中扣除。我给一个数据仓库供参考

   Narration  Credit  Debit  Balance
1  Abc         15.0    Nan    15.0
2  Qwe         NaN     5.0    10.0 
3  Wer         NaN     2.0     7.0  
4  opr         Nan     3.0     5.0
5  Yur         6.0     NaN    11.0

我有这样的数据存储区,但是在此3和4索引借方金额与余额值不匹配的情况下,所以我想要可以与余额匹配的数据存储区。

  Narration  Credit  Debit  Balance
1  Abc         15.0    Nan    15.0
2  Qwe         NaN     5.0    10.0 
3  opr         NaN     3.0     7.0  
4  wer         Nan     2.0     5.0
5  Yur         6.0     NaN    11.0

我想要可以与贷方借记值匹配的数据仓库。例如,它只有5行,所以我有500行,因此我可以检查余额是否与贷方借方金额匹配。

2 个答案:

答案 0 :(得分:2)

如果我对您的理解正确,那么您需要:Series.diffnp.where

我们得到Balance中每一行之间的差异,如果该差异为< 0,则采用absolute并将其分配给Debit

mapping_debit = df.dropna(subset=['Debit']).set_index('Debit')['Narration']
mapping_credit = df.dropna(subset=['Credit']).set_index('Credit')['Narration']

balance = df['Balance'].diff().fillna(df['Credit']).fillna(df['Debit'])

df['Credit'] = np.where(balance.ge(0), balance, np.NaN)
df['Debit'] = np.where(balance.lt(0), balance.abs(), np.NaN)

df['Narration'] = df['Debit'].map(mapping_debit).fillna(df['Credit'].map(mapping_credit))

  Narration  Credit  Debit  Balance
0       Abc    15.0    NaN     15.0
1       Qwe     NaN    5.0     10.0
2       Wer     NaN    3.0      7.0
3       opr     NaN    2.0      5.0
4       Yur     6.0    NaN     11.0

请注意,我们必须导入numpy

import numpy as np

答案 1 :(得分:2)

您还可以尝试以下在某些条件下使用np.selectdf.mask()的情况:

s=df['Credit'].fillna(df['Balance']).diff().abs()
cond1=s.ne(df['Debit'])
cond2=s.eq(df['Debit'].shift(-1))
cond3=s.shift().eq(df['Debit'])

values=np.select([(cond1[:,None]&cond2[:,None]),(cond1[:,None]&cond3[:,None])],
          [df.shift(-1),df.shift()])

final=df.mask(cond1&(cond2|cond3),values)
final['Balance']=df['Balance']

  Narration Credit Debit  Balance
1       Abc     15   NaN     15.0
2       Qwe    NaN     5     10.0
3       opr    NaN     3      7.0
4       Wer    NaN     2      5.0
5       Yur      6   NaN     11.0