I have a issue with applying function for column in pandas, please see below code :
import pandas as pd #create a dict as below data_dic = { "text": ['hello',1,'how are you?',4], "odd": [0,2,4,6], "even": [1,3,5,7] } #create a DataFrame df = pd.DataFrame(data_dic) #define function def checktext(str1): if isinstance(str1,str): return str1.upper() def checknum(str1): if isinstance(str1,int): return str1+1 df['new'] = df['text'].apply(lambda x: checktext(x)) df['new'].head()
my df now show like below:
text odd even new 0 hello 0 1 HELLO 1 1 2 3 None 2 how are you? 4 5 HOW ARE YOU? 3 4 6 7 None
I would like to apply function checknum for 2 cell in column ‘new’ which is having ‘None’ value. Can someone assist this ? Thank you
Advertisement
Answer
IIUC, you can use vectorial code:
# make string UPPER s = df['text'].str.upper() # where there was no string, get number + 1 instead df['new'] = s.fillna(df['text'].where(s.isna())+1)
output:
text odd even new 0 hello 0 1 HELLO 1 1 2 3 2 2 how are you? 4 5 HOW ARE YOU? 3 4 6 7 5
That said, for the sake of the argument, your 2 functions could be combined into one:
def check(str1): if isinstance(str1,str): return str1.upper() elif isinstance(str1,int): return str1+1 df['new'] = df['text'].apply(check)