2

I have a df1 that looks like this:

           Symbol Order  Shares
Date                           
2009-01-14   AAPL   BUY     150
2009-01-21   AAPL  SELL     150
2009-01-21    IBM   BUY     400

And df2 looks like this:

           GOOG AAPL  XOM  IBM Cash
Date                               
2009-01-14  NaN  NaN  NaN  NaN  NaN
2009-01-21  NaN  NaN  NaN  NaN  NaN

I want to move the values in the first DF to the second so that the amount of shares populates under the appropriate stock symbol. So the above would look like:

           GOOG AAPL  XOM  IBM Cash
Date                               
2009-01-14  NaN  150  NaN  NaN  NaN
2009-01-21  NaN  -150 NaN  400  NaN

How would I move all values that I have in my first dataframe to the second dataframe?

4
  • Are you just looking to pivot or actually fill data in another dataframe?: df.pivot(None, 'Symbol', 'Shares') Commented Mar 7, 2019 at 20:28
  • What if a company has BUY and SELL on the same day? Commented Mar 7, 2019 at 20:28
  • @Chris actually fill it. Commented Mar 7, 2019 at 20:30
  • @coldspeed that's a good point, there shoudl actually be only a single line for that in the second df. I am going to edit now . Commented Mar 7, 2019 at 20:30

3 Answers 3

1

You really don't need df2 here. You can compute the result directly from df using some simple reshaping functions set_index, unstack and reindex. You just need the symbols list.

(df.assign(Shares=np.where(df.Order == 'BUY', df.Shares, -df.Shares))
   .drop('Order', 1)
   .set_index('Symbol', append=True)['Shares']
   .unstack(1)
   .reindex(df2.columns, axis=1))  # you can replace df2.columns with a list 

            GOOG   AAPL  XOM    IBM  Cash
Date                                     
2009-01-14   NaN  150.0  NaN    NaN   NaN
2009-01-21   NaN -150.0  NaN  400.0   NaN
Sign up to request clarification or add additional context in comments.

Comments

1

Use np.select to convert numbers to negative if Order == 'SELL' then update

df['Shares'] = np.select([df['Order'] == 'SELL'], [-df['Shares']], df['Shares'])
df2.update(df.pivot(None, 'Symbol', 'Shares'))


            GOOG   AAPL  XOM    IBM  Cash
Date                                     
2009-01-14   NaN  150.0  NaN    NaN   NaN
2009-01-21   NaN -150.0  NaN  400.0   NaN

Comments

0

duckdb

df1.sql.select("*,case when \"order\"='BUY' then 1 else -1 end col1,col1*shares col2").set_alias("tb1").join(df2.sql.set_alias("tb2"),how="right",condition="tb1.date=tb2.date").select("tb1.date,case when Symbol='AAPL' then col2 end AAPL,case when Symbol='GOOG' then col2 end GOOG,case when Symbol='XOM' then col2 end XOM,case when Symbol='IBM' then col2 end IBM,case when Symbol='Cash' then col2 end Cash").aggregate("date,sum(AAPL) AAPL,sum(GOOG) GOOG,sum(XOM) XOM,sum(IBM) IBM,sum(Cash) Cash")

┌────────────┬────────┬────────┬────────┬────────┬────────┐
│    Date    │  AAPL  │  GOOG  │  XOM   │  IBM   │  Cash  │
│  varchar   │ int128 │ int128 │ int128 │ int128 │ int128 │
├────────────┼────────┼────────┼────────┼────────┼────────┤
│ 2009-01-21 │   -150 │   NULL │   NULL │    400 │   NULL │
│ 2009-01-14 │    150 │   NULL │   NULL │   NULL │   NULL │
└────────────┴────────┴────────┴────────┴────────┴────────┘

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.