Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
熊猫(python)中特定列的舍入_Python_Pandas - Fatal编程技术网

熊猫(python)中特定列的舍入

熊猫(python)中特定列的舍入,python,pandas,Python,Pandas,我试图对binance、binanceAmount、livecoin、livecoinAmount、…、profit和procent列进行四舍五入。我从csv导入数据。这是前10行: 这是我的代码: decimals = {} for exchange in usableExchanges: decimals[exchange] = 8 decimals[exchange + "amount"] = 2 df.round(decimals) df.round({'profi

我试图对binance、binanceAmount、livecoin、livecoinAmount、…、profit和procent列进行四舍五入。我从csv导入数据。这是前10行:

这是我的代码:

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


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

您可以分配到字典,并将输出分配到数据帧:

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

您需要分配:
df=df.round({'procent':1,'procent':2})
,等等,将其签回df=df.round({'procent':1,'procent':2})
round
不修改
df
,它返回修改后的
df
。什么是
print(decimals.keys())
print(df.columns)