Home > Software design >  How to apply function for only special cells in column pandas?
How to apply function for only special cells in column pandas?

Time:03-16

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

CodePudding user response:

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)

CodePudding user response:

your function:

def checktext(str1):
    if isinstance(str1,str):
        return str1.upper()

Will return None, if the if statement is false (i.e., 'str1' is not a string). By default, return the value?

def checktext(str1):
    if isinstance(str1,str):
        return str1.upper()
    return str1

CodePudding user response:

First, you could use the StringMethods accessor to convert to upper case whithout any loop. And when you have done that, you can easily process the rows where the result is NaN:

df['new'] = df['text'].str.upper()
mask = df['new'].isna()
df.loc[mask, 'new'] = df.loc[mask, 'text']   1

It gives directly:

           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

CodePudding user response:

Use:

import numpy as np
df['new'] = df['text'].str.upper().fillna(pd.to_numeric(df['text'], 
                                                        errors='coerce'
                                                       )
                                             .add(1).astype(str))\
                      .fillna(df['text'])
print(df)

           text  odd  even           new
0         hello    0     1         HELLO
1             1    2     3           2.0
2  how are you?    4     5  HOW ARE YOU?
3             4    6     7           5.0
  • Related