1

I have data looks like this:

    order_number    ship_distance   delivery_fee    utilization remaining_multiplier_type
     R000001943        3.818           11.25            LOW            DISCOUNT
     R000002604        5.993           7.00             NaN            NaN
     R000005277        2.942           7.00             NaN            NaN
     R000006010        8.289           9.50             NaN            NaN
     R000010127        3.233           6.00             NaN            BASE

I'm trying to round up the value in column ship_distance using the math and numpy.

Using data = math.ceil(data[ship_distance'] I got error TypeError: cannot convert the series to <class 'float'> When using data = np.ceil(data['ship_distance']) I don't get the desired result.

Expected result:

 order_number   ship_distance   delivery_fee    utilization   remaining_multiplier_type 
  R000001943          4            11.25             LOW           DISCOUNT                  
  R000002604          6            7.00              NaN           NaN                       
  R000005277          3            7.00              NaN           NaN                       
  R000006010          9            9.50              NaN           NaN                       
  R000010127          4            6.00              NaN           BASE                      
2
  • 2
    Does this answer your question? Floor or ceiling of a pandas series in python? Commented Sep 9, 2020 at 9:05
  • 1
    All the math functions only work with one number, not a Series (column) or array. What was wrong with the np.ceil? Commented Sep 9, 2020 at 15:33

2 Answers 2

1

Use numpy:

np.ceil(df["ship_distance"]).astype("int64")
Sign up to request clarification or add additional context in comments.

1 Comment

had tried...but I doesn't give me the desired result...Got the following result 27 3.0 1453 13.0 1630 17.0 1857 5.0 2016 1.0 Name: ship_distance, dtype: float64
0
# np.ceil(data['ship_distance'])
data['ship_distance'] = data['ship_distance'].apply(lambda X: int(math.ceil(X)))

print(data)

3 Comments

thanks...it works..why I should use lambda?do you mind explaining?
To convert float to int I used lambda or you can also try the .astype() method to convert an entire pandas column datatype.
There's no real need for the lambda here: math.ceil is already a function that takes a float and returns the ceiling as an int, so data['ship_distance'].apply(math.ceil) would be enough.

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.