熊猫(python)中特定列的舍入
我试图对binance、binanceAmount、livecoin、livecoinAmount、…、profit和procent列进行四舍五入。我从csv导入数据。这是前10行: 这是我的代码:熊猫(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
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)
?