Search code examples
pythonpandasuppercase

Python Upper function


import pandas as pd
df = pd.DataFrame([{"email": "[email protected]"}])
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': '[email protected]'}]

The response I expected :

[{'email': '[email protected]'}]

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   [email protected]
    

    to get the dictionary:

    foo_dict = df.to_dict()
    foo_dict 
    
    {'email': {0: '[email protected]'}}
    

    Block Code:

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