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                      
Nur Atiqah
  • 105
  • 10
  • 2
    Does this answer your question? [Floor or ceiling of a pandas series in python?](https://stackoverflow.com/questions/27592456/floor-or-ceiling-of-a-pandas-series-in-python) – dimay Sep 09 '20 at 09:05
  • 1
    All the `math` functions only work with one number, not a Series (column) or array. What was wrong with the `np.ceil`? – hpaulj Sep 09 '20 at 15:33

2 Answers2

1

Use numpy:

np.ceil(df["ship_distance"]).astype("int64")
dimay
  • 2,768
  • 1
  • 13
  • 22
  • 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 – Nur Atiqah Sep 09 '20 at 09:11
0
# np.ceil(data['ship_distance'])
data['ship_distance'] = data['ship_distance'].apply(lambda X: int(math.ceil(X)))

print(data)
ashraful16
  • 2,742
  • 3
  • 11
  • 32
  • thanks...it works..why I should use lambda?do you mind explaining? – Nur Atiqah Sep 09 '20 at 09:15
  • To convert float to int I used lambda or you can also try the .astype() method to convert an entire pandas column datatype. – ashraful16 Sep 09 '20 at 09:19
  • 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. – Mark Dickinson Sep 10 '20 at 11:16