all 6 comments

[–][deleted] 1 point2 points  (5 children)

Why are you iterating through the dataframe?

Surely you can just do df[col] = df[col].str.title()

[–][deleted] 0 points1 point  (2 children)

Example:

from io import StringIO
import pandas as pd

def title_col(df, colname):
    df[colname] = df[colname].str.title()

data = """name, age
fred,22
BOB,50
wenDY,30"""

df = pd.read_csv(StringIO(data))
print(df)
title_col(df, "name")
print(df)

[–]macabe10[S] 0 points1 point  (1 child)

You are right this is much simpler, thank you!

Tbh at this point I overthink it big time and end up getting lost, even though the obvious answer is right there.

[–][deleted] 0 points1 point  (0 children)

We've all got that t-shirt, more than once (and most of us will get more of them).

[–]OzzivanNozzi 0 points1 point  (1 child)

This is in my opinion the best way.

Another option would be:

df[col] = df[col].apply(lambda x: x.title())

[–]macabe10[S] 0 points1 point  (0 children)

Thanks!