Here's a Python script using the pandas library to capitalize the first letter and lowercase the rest of the text in the 'product' column of a dataframe:

import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({'product': ['APPLE', 'banana', 'Cherry', 'grape']})

# Apply titlecase to the 'product' column
df['product'] = df['product'].apply(lambda x: x.title())

print(df)

Output:

  product
0   Apple
1  Banana
2  Cherry
3   Grape

In this script:

  1. We import the pandas library.
  2. We create a sample dataframe with a 'product' column containing fruit names.
  3. The apply() method applies a lambda function to each element in the 'product' column.
  4. The lambda function uses the title() method to capitalize the first letter of each word and convert the rest to lowercase.
  5. Finally, we print the updated dataframe with the modified 'product' column.
Python Pandas: Capitalize First Letter, Lowercase Rest in Column

原文地址: https://www.cveoy.top/t/topic/nTJO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录