0

I have the following Pandas data frame:

import pandas as pd
df= pd.DataFrame({'type':['Asset','Liability','Asset','Liability','Asset'],'Amount':[10,-10,20,-20,5],'Maturity Date':['2018-01-22','2018-01-22','2018-06-22','2018-06-22','2019-01-22']})

Depending on user input, I wanted to modify the Pandas data frame to only show certain values. For a "date" of 2018-01-31, I would like the data frame to be:

df1= pd.DataFrame({'2018-01-31':[0,0,20,-20,5],'type':['Asset','Liability','Asset','Liability','Asset'],'Amount':[10,-10,20,-20,5],'Maturity Date':['2018-01-22','2018-01-22','2018-06-22','2018-06-22','2019-01-22']})

Similarly for a "date" of 2018-12-31, I would like the data frame to be:

df2= pd.DataFrame({'2018-12-31':[0,0,0,0,5],'2018-01-31':[0,0,20,-20,5],'type':['Asset','Liability','Asset','Liability','Asset'],'Amount':[10,-10,20,-20,5],'Maturity Date':['2018-01-22','2018-01-22','2018-06-22','2018-06-22','2019-01-22']})

Any suggestions for the most efficient way to achieve this?

2 Answers 2

3

User input the date , then we can get it base on this

df['2018-12-31']=(df['Maturity Date']>pd.to_datetime('2018-12-31'))*df.Amount
df
Out[356]: 
   Amount Maturity Date       type  2018-12-31
0      10    2018-01-22      Asset           0
1     -10    2018-01-22  Liability           0
2      20    2018-06-22      Asset           0
3     -20    2018-06-22  Liability           0
4       5    2019-01-22      Asset           5

Another solution using np.where + df.insert

date = '2018-01-31'
df.insert(0, date, np.where(df['Maturity Date'] > '2018-01-31', df.Amount, 0))

df

   2018-01-31  Amount Maturity Date       type
0           0      10    2018-01-22      Asset
1           0     -10    2018-01-22  Liability
2          20      20    2018-06-22      Asset
3         -20     -20    2018-06-22  Liability
4           5       5    2019-01-22      Asset
Sign up to request clarification or add additional context in comments.

2 Comments

Willing to bet this is the fastest one actually. Also, that was my last vote for the day.
@cᴏʟᴅsᴘᴇᴇᴅ a ha , thank you ~ :-) also, np.where looks great !
1

Let's use assign and mask:

print(df)

   Amount Maturity Date       type
0      10    2018-01-22      Asset
1     -10    2018-01-22  Liability
2      20    2018-06-22      Asset
3     -20    2018-06-22  Liability
4       5    2019-01-22      Asset

Add, first column,

input_date = '2018-01-31'
df = df.assign(input_date=df.Amount.mask(df["Maturity Date"] <= input_date,0)).rename(columns={'input_date':input_date})
print(df)

   Amount Maturity Date       type  2018-01-31
0      10    2018-01-22      Asset           0
1     -10    2018-01-22  Liability           0
2      20    2018-06-22      Asset          20
3     -20    2018-06-22  Liability         -20
4       5    2019-01-22      Asset           5

Add, second column,

input_date = '2018-12-31'
df = df.assign(input_date=df.Amount.mask(df["Maturity Date"] <= input_date,0)).rename(columns={'input_date':input_date})
print(df)

   Amount Maturity Date       type  2018-01-31  2018-12-31
0      10    2018-01-22      Asset           0           0
1     -10    2018-01-22  Liability           0           0
2      20    2018-06-22      Asset          20           0
3     -20    2018-06-22  Liability         -20           0
4       5    2019-01-22      Asset           5           5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.