in this commands;
import requests
import pandas as pd
url = "https://api.binance.com/api/v3/depth?symbol=BNBUSDT&limit=100"
payload = {}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data=payload).json()
depth = pd.DataFrame(response, columns=["bids","asks"])
print(depth)
outputs :
........ | bids | asks |
---|---|---|
0 | [382.40000000, 86.84800000] | [382.50000000, 196.24600000] |
1 | [382.30000000, 174.26400000] | [382.60000000, 116.10300000] |
and first i need to change the table this format
........ | bidsValue | bidsQuantity | asksValue | asksQuantity | rangeTotalbidsQuantity | rangeTotalasksQuantity |
---|---|---|---|---|---|---|
0 | 382.40000000 | 86.84800000 | 382.50000000 | 196.24600000 | ||
1 | 382.30000000 | 174.26400000 | 382.60000000 | 116.10300000 |
and then turn the columns values float so that be able to calculate in a specific range values quantity (e.g 0bidsValue 400.00 and ?bidsValue 380.00 ("?" because of i don't know row number) first i must find row number of bidsValue 380.00 (for example it is 59) then calculate 0bidsQuantity to 59bidsQuantity) and last write the results in rangeTotalbidsQuantity column.
I've been trying for hours, I tried many commands from "pandas.Dataframe" but I couldn't do it. Thank you!
CodePudding user response:
You can use this solution. In your case this would look something like this:
depth['bidsValue'], depth['bidsQuantity'] = zip(*list(depth['bids'].values))
depth['asksValue'], depth['asksQuantity'] = zip(*list(depth['asks'].values))
depth = depth.drop(columns=['bids', 'asks'])
For the second part look at this tutorial.
CodePudding user response:
You can use for example:
pd.DataFrame(df['bids'].tolist())
Then concatenate that to the original dataframe.
CodePudding user response:
You can apply()
pandas.Series
and assign to new columns
depth[ ['bidsValue', 'bidsQuantity'] ] = depth['bids'].apply(pd.Series)
depth[ ['asksValue', 'asksQuantity'] ] = depth['asks'].apply(pd.Series)
and later you have to remove original columns bids,asks
depth = depth.drop(columns=['bids', 'asks'])
Full working code with other changes
import requests
import pandas as pd
url = "https://api.binance.com/api/v3/depth"
payload = {
'symbol': 'BNBUSDT',
'limit': 100,
}
response = requests.get(url, params=payload)
#print(response.status_code)
data = response.json()
depth = pd.DataFrame(data, columns=["bids","asks"])
#print(depth)
depth[ ['bidsValue', 'bidsQuantity'] ] = depth['bids'].apply(pd.Series)
depth[ ['asksValue', 'asksQuantity'] ] = depth['asks'].apply(pd.Series)
depth = depth.drop(columns=['bids', 'asks'])
print(depth.to_string())