
Holdings in long format
hardHoldings 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 Name | Type |
|---|---|
| user_id | int64 |
| AAPL | int64 |
| AMZN | int64 |
| NVDA | int64 |
| TSLA | int64 |
holdingsExample Input
| user_id | AAPL | AMZN | NVDA | TSLA |
|---|---|---|---|---|
| 801 | 6 | 0 | 0 | 0 |
| 802 | 0 | 4 | 2 | 0 |
| 803 | 0 | 0 | 1 | 0 |
| 804 | 8 | 0 | 0 | 6 |
| 805 | 0 | 12 | 1 | 0 |
| 806 | 0 | 0 | 0 | 10 |
| 807 | 3 | 2 | 5 | 1 |
Example Output
| user_id | symbol | shares |
|---|---|---|
| 801 | AAPL | 6 |
| 802 | AMZN | 4 |
| 802 | NVDA | 2 |
| 803 | NVDA | 1 |
| 804 | AAPL | 8 |
| 804 | TSLA | 6 |
| 805 | AMZN | 12 |
| 805 | NVDA | 1 |
| 806 | TSLA | 10 |
| 807 | AAPL | 3 |
| 807 | AMZN | 2 |
| 807 | NVDA | 5 |
| 807 | TSLA | 1 |
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.
Company
Robinhood
Difficulty
hard
Topic
reshaping
Language
Pandas
Your code
Loading Python in the background. You can start writing.