Jason
Jason

Reputation: 467

How to select some characters in value of a column and create a new column with this value

I have a csv as below:

Date        File Name        
11/05/2018  CFL200_ABCD
11/06/2018  CFL203_DFSA

I would like to create a new column with values are characters before "_" in the File Name column

Date        File Name        ID
11/05/2018  CFL200_ABCD      CFL200
11/06/2018  CFL203_DFSA      CFL203

How could I do that? Thanks.

Upvotes: 1

Views: 149

Answers (1)

Dishin H Goyani
Dishin H Goyani

Reputation: 7713

Use first split to split string into sub-string and then get item through index

df["ID"] = df["File Name"].str.split('_').str[0]
df
         Date    File Name      ID
0  11/05/2018  CFL200_ABCD  CFL200
1  11/06/2018  CFL203_DFSA  CFL203

Upvotes: 1

Related Questions