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 ?
Asked
Active
Viewed 94 times
-5
-
4What dataframe? What have you tried? – Sin Han Jinn Nov 19 '19 at 06:21
-
https://stackoverflow.com/help/how-to-ask – Yugandhar Chaudhari Nov 19 '19 at 06:22
-
@Hj Sin df['Name'] – Yuvraj Singh Nov 19 '19 at 06:25
-
I want to capitalize first 2 letters of all records in the 'Name' Column of my Dataframe df – Yuvraj Singh Nov 19 '19 at 06:26
-
Please post sample of your dataframe (something like `print(df.head())`), desired output and snippet of your source code that you tried – Chris Nov 19 '19 at 06:30
-
save all of your record into list and then use like below: `[x[0:2].upper() + x[2::] for x in your_list]` – Yeganeh Salami Nov 19 '19 at 06:39
-
@Chris if a name is ysingh then I want to make it YSingh – Yuvraj Singh Nov 19 '19 at 06:43
2 Answers
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

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