Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import requests
- # Load the CSV file into a DataFrame
- tickers = pd.read_csv('CRYPTO.csv')
- # Get unique currencies and tickers
- currencies = tickers['Currency'].dropna().unique() # Remove NaN values from currencies
- tickers_list = tickers['Ticker'].dropna().unique() # Remove NaN values from tickers
- # Initialize an empty DataFrame with tickers as rows and currencies as columns
- pd_frame = pd.DataFrame(index=tickers_list, columns=currencies)
- # Loop through each ticker
- for ticker in tickers_list:
- for currency in currencies:
- try:
- url = f"https://min-api.cryptocompare.com/data/histoday?fsym={ticker}&tsym={currency}&limit=60&aggregate=3&e=CCCAGG"
- response = requests.get(url)
- response.raise_for_status() # Raise HTTPError for bad responses
- data = response.json()
- # Check if the data contains enough entries
- if len(data['Data']) > 0:
- # Get the most recent close price (last entry)
- close_price = data['Data'][60]['close']
- else:
- close_price = None
- print(f"Not enough data for {ticker} in {currency}")
- # Update the DataFrame cell with the close price
- pd_frame.at[ticker, currency] = close_price
- except requests.RequestException as e:
- print(f"Request failed for {ticker} in {currency}: {e}")
- # Print the final DataFrame
- print(pd_frame)
- # Save the DataFrame to a CSV file
- output_csv_path = 'crypto_prices.csv'
- pd_frame.to_csv(output_csv_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement