We help IT Professionals succeed at work.
Get Started

Extract substring from dataframe and add it to new column.

Newton
Newton asked
on
107 Views
Last Modified: 2020-03-02
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')
Comment
Watch Question
Excel & VBA Expert
CERTIFIED EXPERT
Most Valuable Expert 2018
Awarded 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE