Holdings in long format

reshaping

Holdings in long format

Robinhood Pandas Interview Question

Robinhood stores a snapshot of each user's holdings with one column per stock. The analytics team needs the same data as one row per user and stock.

Using the holdings DataFrame, return one row for every stock a user holds, with the user_id, the stock's ticker as symbol and the number of shares. Leave out stocks the user holds no shares of. Sort the rows by user_id, then by symbol from A to Z. Assign the answer to result.

Asked of

  • Data Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist
  • ML Engineer

holdingsDataFrame7 rows

Column NameType
user_idint64
AAPLint64
AMZNint64
NVDAint64
TSLAint64

holdingsExample Input

user_idAAPLAMZNNVDATSLA
8016000
8020420
8030010
8048006
80501210
80600010
8073251

Example Output

user_idsymbolshares
801AAPL6
802AMZN4
802NVDA2
803NVDA1
804AAPL8
804TSLA6
805AMZN12
805NVDA1
806TSLA10
807AAPL3
807AMZN2
807NVDA5
807TSLA1

Explanation

User 804 holds 8 shares of AAPL and 6 of TSLA, so that user gets two rows. Stocks with no shares, such as user 801's AMZN, NVDA and TSLA, are left out.

The example above is a small slice of the data. Your code runs against the full DataFrames.