pd.Series.str.upper function under the df.apply() is showing me the Error. On the other hand pd.Series.min is running perfectly

113 Views Asked by At

When I use df.apply(pd.Series.str.upper) shows me an error -

Although df.apply(pd.Series.min) is running absolutely fine! and df.apply(lambda x: x.str.upper()) is running fine too.

df = pd.DataFrame(
    {
     "Name":[
         "Harry","Sam", "Jack"], "Gender": ["M","M","F"]})


df.apply(pd.Series.str.lower)
Error - Series' object has no attribute '_inferred_dtype'
2

There are 2 best solutions below

0
HedgeHog On

Simply adapt your upper() approach, it should give you the expected result:

df.apply(lambda x: x.str.lower())
0
Abhi On

When you apply pd.Series.str its converting each row to String Series type hence lower method would not work unless you apply individually like below

df = pd.DataFrame(
    {
     "Name":[
         "Harry","Sam", "Jack"], "Gender": ["M","M","F"]
    }
)
df.apply(pd.Series.str) # Check output in below image

Appliying_Series_Str

Its clear if you want to apply lower you have to iteratively apply at every instance hence 'lambda' would be useful

df.apply(lambda x: x.str.lower())