xxxxxxxxxx
import pandas as pd
# reading csv
s = pd.read_csv("stock.csv", squeeze = True)
# defining function to check price
def fun(num):
if num<200:
return "Low"
elif num>= 200 and num<400:
return "Normal"
else:
return "High"
# passing function to apply and storing returned series in new
new = s.apply(fun)
# printing first 3 element
print(new.head(3))
# printing elements somewhere near the middle of series
print(new[1400], new[1500], new[1600])
# printing last 3 elements
print(new.tail(3))
xxxxxxxxxx
# N. B. myfunction must be able to take as argument the elements of the list
def myfunction(x):
# process x
return #some results
# declare your list
l1 = list( )
# get a list with the transformed values according to myfunction
l2 = list(map(myfunction, l1))
xxxxxxxxxx
apply is an old-school1 way of unpacking arguments. In other words, the following all yield the same results:
results = apply(foo, [1, 2, 3])
results = foo(*[1, 2, 3])
results = foo(1, 2, 3)
so instead of writing "apply(fn, args, kwargs)" use "fn(*args, **kwargs)"