0

I would like to format the DataFrame

Col1 Col2 Col3
-0.012 3.2 nan
0 -1 15.2
0.5 7.53 76.88

using trailing zeros, such that all values in each column have the same number of digits. The result should look like this:

Col1 Col2 Col3
-0.012 3.20 nan
0.000 -1.00 15.20
0.500 7.53 76.88

In the initial DataFrame, all values are strings

2
  • Have you tried formatting the columns? df['col1'] = df['col1'].map('${:,.3f}'.format) Commented Aug 31, 2022 at 15:52
  • @Michael, did the solution for worked for you? Commented Sep 28, 2022 at 23:58

1 Answer 1

0
df.applymap(float)
      Col1   Col2    Col3
1   -0.012   3.20     NaN
2    0.000  -1.00   15.20
3    0.500   7.53   76.88

data use

data={'Col1': {1: '-0.012 ', 2: '0', 3: '0.5'},
 'Col2': {1: '3.2', 2: '-1', 3: '7.53'},
 'Col3': {1: np.nan, 2: '15.2', 3: '76.88'}}

df=pd.DataFrame(data)
df

    Col1    Col2    Col3
1   -0.012    3.2    NaN
2    0       -1     15.2
3    0.5     7.53   76.88
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.