Home > Enterprise >  add specific part of one column values to another column
add specific part of one column values to another column

Time:01-09

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?

CodePudding user response:

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

CodePudding user response:

You can construct a simple function and use apply to call the function whenever you want.

def add_prefix(row):
    prefix = row["existing_indiv"].split(".")[0]   "."
    return f"{prefix}{row['queries']}"
    
df['queries'] = df.apply(add_prefix, axis=1)

print(df)

Output:

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