Search code examples
pythonpandasdata-scienceanalytics

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


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


Solution

  • 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())