1

I am making an API call to coinmarketcap.com and convert JSON object that is being returned to pandas data frame. I was wondering if there any way to store the data frame columns as constant and use it outside of the function?

def get_cmc_supply(): url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest' parameters = { 'start': '13', 'limit': '13', 'convert': 'USD', } header = { 'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': '###################', } data = requests.get(url, headers = header,timeout=10).json() records = [] for item in data["data"]: records.append( { "name": item['symbol'], "supply": item['circulating_supply'], } ) df = pd.DataFrame.from_records(records) df = df.rename(columns={"name":"symbol"}) df = df.set_index('symbol') return df get_cmc_supply() 

How can I store df, so I can access it outside of the function without calling the get_cmc_supply() function again and getting a new supply data from API call?

5
  • What do you mean store it as 'constant'? You can write it to a file with to_csv() if that's what you're asking Commented Apr 23, 2019 at 22:36
  • By 'constant' I mean to log the values that were returned from the API call. Is there any way without writing it to .csv file? Commented Apr 23, 2019 at 22:39
  • Once its a DataFrame you can reference any column. If there is a DataFrame called df, df['columnname'] will call the values in the column called columnname. Commented Apr 23, 2019 at 22:44
  • If you search on the phrase "Python read JSON into Pandas", you’ll find resources that can explain it much better than we can in an answer here. Commented Apr 23, 2019 at 22:45
  • I wasn't able to find anything that would solve it. Commented Apr 23, 2019 at 22:57

1 Answer 1

1

If I understand, you are looking to save the DataFrame outside the function so that you don't have to re-create it again.

Set the dataframe outside the function.

df = get_cmc_supply() 

Now you can use it whenever you wish.

If you want to store it for future use after you close the program, you can use pickle.

Pickle the dataframe (df) for use another time

filename = "your_file_name.pickle" with open(filename, "wb") as f: pickle.dump(df, f) 

Use this to view the "your_file_name.pickle" file. Open the existing raw data from the pickle file.

filename = "your_file_name.pickle" infile = open(filename, "rb") df = pickle.load(infile) infile.close() 

If you are truly looking to make the dataframe immutable, check out static frame.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your solution. Is there any way to make it (static) without using pickle or static frame?
Tell me what you mean by 'static'?
I'm just reading your comments above as well and I'm not sure what you are trying to accomplish. Could you clarify what you are trying to do? 'constant' and 'static' could be interpreted different ways. Thanks!
Let's say, for example the above function returns the following data frame: supply symbol BTC 1.766170e+07 ETH 1.057840e+08 XRP 4.200497e+10 BCH 1.774465e+07 EOS 9.428902e+08 LTC 6.147143e+07 BNB 1.411755e+08 USDT 2.676666e+09 XLM 1.936421e+10 ADA 2.592707e+10 How can I store these values? Because if I want to access this data frame outside of the function I would do df = get_cmc_supply(), which would run the function again and make another API call and new json object with new data is going to be returned. Thanks
Once you run the function by calling df = get_cmc_supply(), the function will return a dataframe and the df you used will be the dataframe. Try typing df.head() below the function call. or print(df.head()) You should see the results of the function call.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.