How can I select the Top 10 coins (by descending order) of the below dataframe for each given day?
I am attaching a code sample for generating a dummy sample
import pandas as pd
import yfinance as yf
df = pd.DataFrame()
for i in ['BTC-USD', 'ETH-USD', 'XRP-USD']:
ticker = yf.Ticker(i)
data = ticker.history(period="max")
data.reset_index('Date', inplace=True)
data['value'] = np.random.random_sample()
data['coin'] = i
data.drop(columns=['Open', 'High', 'Low', 'Close', 'Volume', 'Dividends', 'Stock Splits'], inplace=True)
data.columns = ['date', 'coin', 'value']
data.set_index(['date', 'coin'], inplace=True)
df = df.append(data)
I am looking for an output format like :
date | Coins |
---|---|
2020-10-10 | [bitcoin, ethereum, polkadot, ..] |
2020-10-11 | [polkadot, ethereum, tether, ..] |
CodePudding user response:
You can use this code:
df['value'].groupby(level=0).nlargest(10).reset_index(level='coin')['coin'].groupby(level=0).agg(list)