I have a dataframe DF with just 1 column and I want to uppercase first 2 letters of all the records in python. how do I do that?
You may use map and Convert you data as you required:
try below:
import pandas as pd
df = pd.DataFrame({'name':['geeks', 'gor', 'geeks', 'is','portal', 'for','geeks']})
df['name']=df['name'].map(lambda x: x[:2].upper()+x[2:])
print (df)
output:
name
0 GEeks
1 GOr
2 GEeks
3 IS
4 POrtal
5 FOr
6 GEeks
If you want to implement what Yeganeh suggested in the comments,
list = []
for items in df['Name']:
list.append(items)
newlist = ([x[0:2].upper() + x[2::] for x in list])
print(df.replace(list,newlist))
Replace the previous list with the new one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.