在pandas(python)中舍入特定列

2024-09-26 18:18:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着把binance,binanceAmount,livecoin,livecoinAmount,…,procent列四舍五入。我从csv导入数据。这是前10行:

enter image description here 这是我的代码:

decimals = {}
for exchange in usableExchanges:
    decimals[exchange] = 8
    decimals[exchange + "amount"] = 2


df.round(decimals)
df.round({'profit': 1, 'procent': 2})

Tags: csv数据代码indfforexchangebinance
1条回答
网友
1楼 · 发布于 2024-09-26 18:18:20

您可以分配给字典并将输出分配给DataFrame

decimals = {'profit': 1, 'procent': 2}
for exchange in usableExchanges:
    decimals[exchange] = 8
    decimals[exchange + "Amount"] = 2

df = df.round(decimals)
print (df)

样本

np.random.seed(2019)

cols = ['binance','binanceAmount','kucoin','kucoinAmount','profit','procent']
df = pd.DataFrame(np.random.randint(10, size=(4,6)), columns=cols) / 2.34
print (df)
    binance  binanceAmount    kucoin  kucoinAmount    profit   procent
0  3.418803       0.854701  2.136752      3.418803  2.564103  3.418803
1  0.000000       0.000000  2.991453      3.418803  2.136752  1.282051
2  0.000000       0.854701  2.136752      2.991453  3.418803  2.136752
3  1.709402       0.000000  0.427350      2.564103  0.000000  0.854701

usableExchanges = ['binance','kucoin']

decimals = {'profit': 1, 'procent': 2}
for exchange in usableExchanges:
    decimals[exchange] = 8
    decimals[exchange + "Amount"] = 2

df = df.round(decimals)
print (df)
    binance  binanceAmount    kucoin  kucoinAmount  profit  procent
0  3.418803           0.85  2.136752          3.42     2.6     3.42
1  0.000000           0.00  2.991453          3.42     2.1     1.28
2  0.000000           0.85  2.136752          2.99     3.4     2.14
3  1.709402           0.00  0.427350          2.56     0.0     0.85

相关问题 更多 >

    热门问题