Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python apply function to list

# 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))
Comment

python apply function

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))
Comment

PREVIOUS NEXT
Code Example
Python :: how to access pandas column 
Python :: convert ipynb to py 
Python :: string slicing python 
Python :: pairs with specific difference 
Python :: Python list function tutorial 
Python :: map numpy array 
Python :: interviewbit with Python questions solutions 
Python :: frequency meaning 
Python :: length of queue python 
Python :: Python NumPy delete Function Example 
Python :: Python RegEx Subn – re.subn() Syntax 
Python :: what is xarray 
Python :: length of list in python 
Python :: python loop until condition met 
Python :: multivaluedictkeyerror django 
Python :: function composition python 
Python :: date and time using tkinter 
Python :: how to remove a string in python 
Python :: // in python means 
Python :: how to use python all() function to check a list is empty or not 
Python :: deque python 
Python :: python read array line by line 
Python :: count function in python 
Python :: how to import packages in python 
Python :: pythonanywhere django 
Python :: django cache framework 
Python :: how to console log in django heroku 
Python :: python class optional attributes 
Python :: metodo de clase python 
Python :: python script to execute shell azure cli commands in python 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =