Write a program in Python to round all the elements in a given series

PythonPandasServer Side ProgrammingProgramming

Input

Assume, you have a Series,

0    1.3
1    2.6
2    3.9
3    4.8
4    5.6

Output

0    1.0
1    3.0
2    4.0
3    5.0
4    6.0

Solution 1

  • Define a Series

  • Create an empty list. Set the for loop to iter the data. Append round of values to the list.

  • Finally, add the elements to the series.

Example

Let us see the complete implementation to get a better understanding −

import pandas as pd
l = [1.3,2.6,3.9,4.8,5.6]
data = pd.Series(l)
print(data.round())

Output

0    1.0
1    3.0
2    4.0
3    5.0
4    6.0

Solution 2

Example

import pandas as pd
l = [1.3,2.6,3.9,4.8,5.6]
data = pd.Series(l)
ls = []
for i,j in data.items():
   ls.append(round(j))
result = pd.Series(ls)
print(result)

Output

0    1
1    3
2    4
3    5
4    6
raja
Published on 10-Feb-2021 13:25:11
Advertisements