0

The following is my dataframe temp

aadt fs speed_limit
5580 3 0
0 7 0
1750 3 10
0 7 0
3940 4 0
470 5 0
0 7 0
0 7 0
df = pd.DataFrame({
    'aadt'       : [5580, 0, 1750, 0, 3940, 470, 0, 0],
    'fc'         : [3, 7, 3, 7, 4, 5, 7, 7],
    'speed_limit': [0, 0, 10, 0, 0, 0, 0, 0]
})

I'm trying to replace the zeros in speed_limit column with a function of 'fs' column. The function is 38 + temp['fs']. However, speed_limit can take only certain values from mylist.

The following code works well without temp['fs']. However, adding temp['fs'] raises the ValueError. Any suggestions are welcome.

myList = [15, 30, 35, 40, 45, 50, 60, 70]

temp['speed_limit'] = np.where(
    temp['speed_limit']==0
    ,min(filter(lambda i: i > (38 + temp['fs']), myList))
    ,temp['speed_limit'])
wjandrea
  • 28,235
  • 9
  • 60
  • 81
ALI
  • 3
  • 1
  • 2
  • `temp['speed_limit']==0` This comparison makes no sense. `temp['speed_limit']` is a _column_ containing many values. Some are equal to zero and some aren't. It makes no sense to ask "is this column equal to zero". – John Gordon Mar 25 '23 at 02:55
  • 1
    @JohnGordon. No it makes sense. The problem is the `filter` part – Corralien Mar 25 '23 at 06:00

0 Answers0