Search code examples
pythonpandasuppercase

Python Upper function


import pandas as pd
df = pd.DataFrame([{"email": "test@gmail.com"}])
is_upper = lambda x: x.upper() if isinstance(x, str) else x
df = df.applymap(trim_strings)
a = df.to_dict("records")

The response I get :

[{'email': 'test@gmail.com'}]

The response I expected :

[{'email': 'TEST@GMAIL.COM'}]

What can be the issue here?


Solution

  • To get the expected output, consider try this:

    df['email'] = df['email'].str.upper()
    

    Your df:

        email
    0   TEST@GMAIL.COM
    

    to get the dictionary:

    foo_dict = df.to_dict()
    foo_dict 
    
    {'email': {0: 'TEST@GMAIL.COM'}}
    

    Block Code:

    import pandas as pd
    df = pd.DataFrame([{"email": "test@gmail.com"}])
    df['email'] = df['email'].str.upper()
    foo_dict = df.to_dict()