-5

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 ?

Yuvraj Singh
  • 37
  • 1
  • 1
  • 6

2 Answers2

1

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

demo

Divyesh patel
  • 967
  • 1
  • 6
  • 21
0

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.

Sin Han Jinn
  • 574
  • 3
  • 18