简体   繁体   中英

Python - String Formatting (How to Limit Decimal Without the Float Getting Converted Into String)

df = pd.DataFrame(np.random.randn(10).reshape(5,2), index =['a','b','c','d','e'], columns = ['one', 'two'])

convert_decimal = lambda x: '{:.1f}'.format(x)

df = df.applymap(convert_decimal)

df

Error: TypeError Traceback (most recent call last) in ----> 1 abs(df)

~\Anaconda3\lib\site-packages\pandas\core\generic.py in abs (self) 1498 1499 def abs (self): -> 1500 return self.abs() 1501 1502 def round (self, decimals=0):

~\Anaconda3\lib\site-packages\pandas\core\generic.py in abs(self)
9480 3 7 40 -50 9481 """ -> 9482 return np.abs(self) 9483 9484 def describe(self, percentiles=None, include=None, exclude=None):

TypeError: bad operand type for abs(): 'str'

It looks like you could just cast the lambda computation as a float.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM