Displaying decimals as a JSON date pandas
As you’re using the JSON_LOAD' METHOD from pandas to parse a json feed, there might be a limitation when dealing with decimal points. The ISSUE ARISES BECAUUS SOME APIS RETURN DATA with CommAS as Thinkousand Separators, While Others use Dots (.) Instead.
Here is an article on how all the decimals become all of the JSON feed when you use a panda to change the date:
Problem Information
When you use theJSON_LOADto structure a JSON feed that contains decimals such as shares or financial information, you may face problems with these numbers correctly. In particular, the code seeks to deal with cases where the application interface returns data by commas () in thousands of separators.
Solution: Usingpd.read_json ()pars_dates = True
with
To solve this problem, we can use thepandas.read_json ()function with
pars_dates = Trueparameter. This approach gives us the opportunity to structure JSON files in the Pandas data frame without losing any information.
Python
Bring pandas pd
Replace ' with real API URL
Url = '
Def Fetch_json_data (URL, Params):
"" "
Search JSON information from the specified URL and return them to the Panda data frame.
Parameters:
URL (STR): API end point URL.
Parameters (DICT): Question parameters dictionary to filter the answer.
Returns:
PD.Dataframe: Dataframe containing JSON information retrieved.
"" "
Set API request parameters
params = {k: v f: k, v params.items (
Search JSON data with panda input-builtRead_json ()function
Data = pd.read_json (url, params = params)
Refund information
Def Main ():
Url = '
params = {'symbol': 'BTCUSDT', 'Interval': '1M', '' border ': 1000}
adjust these parameters according to your needs
Data = fetch_json_data (url, params)
Print (data)
If __nimi__ == '__Main__':
Main ()
How does it work:
Fetch_json_Data ()
function takes the API end point URL and query parameters as feed. First, convert the query parameters into a dictionary with only non-numbers (timestamp
,open ', "high", "low",
close) that are useful in filtering responsibility.
Then we use thepd.read_json ()JSON information from the specified URL with these filtered parameters. The resulting data frame is then printed in the console.
tips and variations:
- You can customize the application subscription parameters according to your special needs.
- Be aware of the restrictions on interest rates to avoid excessive requests.
- If you are dealing with large data forces, consider sharing JSON data into smaller pieces using thechunksize = 10000
.
- To display decimal values as floating instead of integers, simply change theint ()
float ()` pandas information frame.