Pandas create a new column based on string-matching values from another column

Question:

Is there any way to create a new column based on string-matching values from another column without duplicating the column and replacing the values?

So far this is one solution that I am using:


df = pd.DataFrame({
        "Task": ["T1", "T1", "T2", "T3","T3"],
        "Complexity": [1, 2, 3, 2, 1],
        "Status": ["OpenA", "ClosedB", "ClosedC", "OpenC", "OpenA"]})

df['Statno']=df['Status']
df=df.replace({'Statno':{'(?i).*Open.*': '1', '(?i).*Close.*' : '0' }}, regex=True)
df
Asked By: d8888d

||

Answers:

like that?

import pandas as pd

df = pd.DataFrame({
        "Task": ["T1", "T1", "T2", "T3","T3"],
        "Complexity": [1, 2, 3, 2, 1],
        "Status": ["OpenA", "ClosedB", "ClosedC", "OpenC", "OpenA"]})

df['Statno']=df['Status'].replace({'(?i).*Open.*': '1', '(?i).*Close.*' : '0' }, regex=True)
df
Answered By: user1889297
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.