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


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

Updated on: 24-Feb-2021

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements