xavi
xavi

Reputation: 80

add specific part of one column values to another column

I have the following dataframe

import pandas as pd

data = {'existing_indiv': ['stac.Altered', 'MASO.MHD'], 'queries': ['modify', 'change']}
df = pd.DataFrame(data)

    existing_indiv     queries
0   stac.Altered       modify
1   MASO.MHD           change

I want to add the period and the word before the period to the beginning of the values of the queries column

Expected outcome:

    existing_indiv     queries
0   stac.Altered       stac.modify
1   MASO.MHD           MASO.change

Any ideas?

Upvotes: 1

Views: 65

Answers (1)

akuiper
akuiper

Reputation: 215117

You can use .str.extract and regex ^([^.]+\.) to extract everything before the first .:

df.queries = df.existing_indiv.str.extract('^([^.]+\.)', expand=False) + df.queries

df
  existing_indiv      queries
0   stac.Altered  stac.modify
1       MASO.MHD  MASO.change

If you prefer .str.split:

df.existing_indiv.str.split('.').str[0] + '.' + df.queries

0    stac.modify
1    MASO.change
dtype: object

Upvotes: 3

Related Questions