Avatar of Newton
Newton
 asked on

Extract substring from dataframe and add it to new column.

How to extract a substring from column and add it back as new column.

For example, I have file1 with 2 column. I want extract first 3 character from fullname column and then I need to add it as new column.

File1
FullName|count
APPLE|100
ORANG|200
GRAPES|300

I need below output
Output1
FullName|ShortName|count
APPLE |APP|100
ORANGE |ORA|200
GRAPES|GRA|300

Is below is correct way of doing this.
Script
df = pd.read_csv('File1', sep='|')
df['ShortName'] = df['FullName'].str.slice[0:3]
df.out_csv('output1')
Python

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Newton

ASKER
Thank You Neeraj. Your solution solved my problem.
Subodh Tiwari (Neeraj)

You're welcome Newton! Glad it worked as desired.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck