I want to captalize strings using lambda function (from a df where columns dtypes are object and int)

Viewed 112

my code:

if df.dtypes=='object':
    df.applymap(lambda x: x.upper())

Error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

2 Answers

You were trying to convert the dataframe altogether. Use this code:

for col in df.columns:
    if df[col].dtype=='object':
        df[col] = df[col].str.upper())

Hope that works! Because I have personally checked this right now..

A df.dtypes is not a scalar if your DataFrame has more than one columns. So it fails the condition test if df.dtypes=='object'.

Try the following code:

for c in df.columns:
    if df[c].dtype=='object':
        df[c] = df[c].apply(lambda x: x.upper())
Related