How to extract a string from a Pandas dataframe and create a new column

88 Views Asked by At

I start from a pandas dataframe with this columns:

Name:    'Computer', 'Tablet', 'Monitor'
Price:   900, 300, 400
Identifier:  '11$10qw-IDAA', '2222-IL$DB123', '2237-12$33AVD-ewwfq'

I would like to create a new column (for example a NewString) of the data frame which extracts substrings from the Identifier column that contain the $ character. That is, I would like to add the column with the values to the df:

NewString
11$10qw
IL$DB123
12$33AVD

I tried to do it with two nested for loops: one that loops over all the rows of the df, and inside another loop that works on the list formed by the split on the "Identifier" column, but I can't solve it

1

There are 1 best solutions below

0
mozway On

Use a regex with str.extract:

df['out'] = df['Identifier'].str.extract(r'([^-]*\$[^-]*)', expand=False)

Output:

       Name  Price           Identifier       out
0  Computer    900         11$10qw-IDAA   11$10qw
1    Tablet    300        2222-IL$DB123  IL$DB123
2   Monitor    400  2237-12$33AVD-ewwfq  12$33AVD