Skip to content
Advertisement

Creating a list merging the elements of a previous list and the values of a specific column of a data frame

I want to create a new list merging the elements of a previous list plus the values of a specific column of one data frame. See the example below.

The list I already have:

variables = ['Price_x_x', 'Mkt Cap_x', '24h Volume_x','Contributors_x', 'Commits in Last 4 Weeks_x','Reddit Subscribers_x', 'FB Likes_x','Market Cap Dominance']

The column:

0        BTC
1        ETH
2        BNB
3       USDT
4        DOT
       ...  
995      CPC
996      BLK
997    ROUTE
998      CNN
999     BULL
Name: COIN, Length: 1000, dtype: object

The desired output:

['BTC_Price_x_x', 'BTC_Mkt Cap_x','BTC_24h Volume_x',...,'BULL_FB Likes_x','BULL_Market Cap Dominance']

Advertisement

Answer

IIUC, just loop over them:

result = []
for ticker in df['desired_col'].unique():
    for variable in variables:
        result.append(f"{ticker}_{variable}")
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement