Ask Your Question
0

How can I make the first letter of every word in a dataframe column capitalized?

asked 2022-12-18 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-25 13:00:00 +0000

huitzilopochtli gravatar image

You can use the str.title() method to capitalize the first letter of every word in a dataframe column. Here's an example:

import pandas as pd

# create a dataframe with a column of strings
df = pd.DataFrame({'text': ['hello world', 'this is a test', 'data science is fun']})

# apply the title method to the 'text' column
df['text'] = df['text'].str.title()

# print the updated dataframe
print(df)

Output:

                  text
0          Hello World
1       This Is A Test
2  Data Science Is Fun

In this example, the str.title() method is applied to the 'text' column using the df['text'].str accessor. This automatically applies the method to each string in the column. The updated 'text' column is then assigned back to the dataframe using df['text'] =, and the resulting dataframe is printed.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-18 11:00:00 +0000

Seen: 10 times

Last updated: Sep 25 '21