Home > other >  Fill new column in dataframe using conditional logic
Fill new column in dataframe using conditional logic

Time:10-26

I'm a given the condition to fill a new column on a string which was Game [Type A, Type B, Type C]: Played, ELSE: Status, the idea is this if Game column has any of the items in the list, which is Type A or Type B or Type C then the new column should have the string Played and if Game has other strings which are not specified in the list, then new column should have the value in corresponding column Status.

full_string = "Game [Type A, Type B, Type C]: Played, ELSE: Status"

From the full string, I've managed to get the options list and column names and else option. The are in the following variables:

options = ['Type A', 'Type B', 'Type C'] #this is a list
col_to_check = 'Game'
value_if_met = 'Played'
else_use_col = 'Status' new_col_name = 'Result'

I don't know how I can fill the new_col_name using the above data for the following dataframe?

df = pd.DataFrame(
    {
            'ID': ['AB01', 'AB02', 'AB03', 'AB04', 'AB05','AB06'],
            'Game': ['Type A','Type B','Type A','Type C','Type D','Type D'],
            'Status': ['Won', 'Draw', 'Won', np.nan, 'Won',np.nan]
    }
    )  

The new column should have 'Result':['Played','Played','Played','Played','Won',np.nan] for the above dataframe.

CodePudding user response:

You can use numpy.where function, to get the required values; use .isin method to check if the value of column Game is one of [Type A, Type B, Type C], assign Played for True values, and assign Status column values for False values:

>>> np.where(df['Game'].isin(['Type A', 'Type B', 'Type C']), ['Played'], df['Status'])
array(['Played', 'Played', 'Played', 'Played', 'Won', nan], dtype=object)

You can assign it as a new column:

df['Result'] = np.where(df['Game'].isin(['Type A', 'Type B', 'Type C']),
                        ['Played'],
                        df['Status'])

     ID    Game Status  Result
0  AB01  Type A    Won  Played
1  AB02  Type B   Draw  Played
2  AB03  Type A    Won  Played
3  AB04  Type C    NaN  Played
4  AB05  Type D    Won     Won
5  AB06  Type D    NaN     NaN
  • Related