Ravanelli
Ravanelli

Reputation: 95

create new columns on pandas based on one column elements

I have a dataframe with column like this:

    column_1                    
0   0.25 / 0 / 0.25 / -0.25     
1   -0.25 / 0 /1                
2   0 / -0.5 / -0.25            
3   1/ 0.25 / -0.75             

each row is made of chain of consecutive numbers (separated by /) I want to create 2 new column, and keep only the first element and the last element, like below example

    column_1                     new_column_1st_element   new_column_last_element
0   0.25 / 0 / 0.25 / -0.25      0.25                     -0.25
1   -0.25 / 0 /1                 -0.25                    1
2   0 / -0.5 / -0.25             0                        -0.25
3   1/ 0.25 / -0.75              1                        -0.75

Upvotes: 0

Views: 48

Answers (3)

Shubham Sharma
Shubham Sharma

Reputation: 71689

Use, Series.str.extract along with the given regex pattern:

df[['first', 'last']] = df['column_1'].str.extract(r'([^/]+).*?([^/]+)$')

Result:

# print(df)

column_1                    first     last
0  0.25 / 0 / 0.25 / -0.25   0.25    -0.25
1             -0.25 / 0 /1  -0.25        1
2         0 / -0.5 / -0.25      0    -0.25
3          1/ 0.25 / -0.75      1    -0.75

You can test the regex pattern here.

Upvotes: 1

Balaji Ambresh
Balaji Ambresh

Reputation: 5037

Here you go:

df['new_column_1st_element'] = df.column_1.str.split('/').str[0]
df['new_column_last_element'] = df.column_1.str.split('/').str[-1]

Output

                  column_1 new_column_1st_element new_column_last_element
0  0.25 / 0 / 0.25 / -0.25                  0.25                    -0.25
1             -0.25 / 0 /1                 -0.25                        1
2         0 / -0.5 / -0.25                     0                    -0.25
3          1/ 0.25 / -0.75                      1                   -0.75

Upvotes: 1

Vishal Dhawan
Vishal Dhawan

Reputation: 351

Assuming column_1 has data in string datatype

df['new_column_1st_element'] = df.apply(lambda row: row['column_1'].split('/')[0], axis = 1)

Similarly this can be done for the new_column_last_element

Upvotes: 1

Related Questions