我如何在同一列中与Python中的单独列中进行两个值​

发布于 2025-01-26 09:10:10 字数 2497 浏览 1 评论 0原文

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)

输出:

........投标询问
0[382.40000000,86.84800000][382.50000000,196.24600000]
1[382.30000000,174.26400000][382.60000000,116.10300000],

并且首先我需要更改桌子

。 ..bidsValuebidsQuantityasksValueasksQuantityrangeTotalbidsQuantityrangeTotalasksQuantity
0382.4000000086.84800000382.50000000196.24600000
1382.30000000174.26400000382.60000000116.10300000

and then turn the columns values float so that be able to calculate in a specific range values quantity (例如0bidsvalue 400.00和?bidsvalue 380.00(“?”,因为我不知道行号)首先,我必须找到BidsValue 380.00的行号(例如,它是59),然后计算0bidssquantity to 59 bidsquantity to 59 bidsquantity 和Last 在rangetalbidsquantity列中写下结果

我已经尝试了几个小时,我尝试了许多来自“ pandas.dataframe”的命令,但我做不到。 谢谢你!

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 :

........bidsasks
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

........bidsValuebidsQuantityasksValueasksQuantityrangeTotalbidsQuantityrangeTotalasksQuantity
0382.4000000086.84800000382.50000000196.24600000
1382.30000000174.26400000382.60000000116.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

◇流星雨 2025-02-02 09:10:10

您可以使用此解决方案
在您的情况下,这将看起来像这样:

depth['bidsValue'], depth['bidsQuantity'] = zip(*list(depth['bids'].values))
depth['asksValue'], depth['asksQuantity'] = zip(*list(depth['asks'].values))
depth = depth.drop(columns=['bids', 'asks'])

第二部分查看此教程

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.

绿光 2025-02-02 09:10:10

您可以使用:

pd.DataFrame(df['bids'].tolist())

然后将其连接到原始数据框架上。

You can use for example:

pd.DataFrame(df['bids'].tolist())

Then concatenate that to the original dataframe.

奶茶白久 2025-02-02 09:10:10

您可以apply() pandas.Series并分配给新列

depth[ ['bidsValue', 'bidsQuantity'] ] = depth['bids'].apply(pd.Series)
depth[ ['asksValue', 'asksQuantity'] ] = depth['asks'].apply(pd.Series)

,然后您必须删除原始列bids,ask ass ass

depth = depth.drop(columns=['bids', 'asks'])

与其他其他工作代码完整的工作代码更改

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())

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())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文