show me python script how to change the first letter into capitle and res lower cast for prodhct column in df
Here's an example Python script that uses the pandas library to change the first letter of the "product" column in a dataframe to uppercase and the rest to lowercase:
import pandas as pd
# create sample dataframe
df = pd.DataFrame({'product': ['APPLE', 'banana', 'Cherry', 'grape']})
# apply titlecase to 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, we first import the pandas library. Then, we create a sample dataframe with a "product" column containing some fruit names.
We then use the apply() method to apply a lambda function to each element in the "product" column. The lambda function uses the title() method to capitalize the first letter of each word in the string and convert the rest to lowercase.
Finally, we print the updated dataframe with the modified "product" column
原文地址: https://www.cveoy.top/t/topic/eCkJ 著作权归作者所有。请勿转载和采集!