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.
Asked: 2022-12-18 11:00:00 +0000
Seen: 10 times
Last updated: Sep 25 '21