Creating new column in dataframe based on conditions in 2 other columns I would like to create a new column in my dataframe based on values from both the gender and experimental_grouping columns. 

As I have it written below, the column df['group_gender'] has 'control_m' in every row. 

Any ideas on why the if statement isn't working/a better way to accomplish this goal?

def group_and_gender(group):
    for i in group:
        if i == 'Controls':
            for i in df['gender']:
                if i == 'M':
                    df['group_gender'] = 'control_m'
                else: 
                    df['group_gender'] = 'control_f'
        else:
            for i in df['gender']:
                if i == 'M':
                    df['group_gender'] = 'dp_m'
                else: 
                    df['group_gender'] = 'dp_f'

group(df['experimental_grouping'])
-the problem with an inaccurate filling of column group_gender is that in df['group_gender'] = 'dp_m' in the following code, if i == 'M' you are filling the whole column with dp_m, instead you should use methods like iloc but it is not really an efficient way specifically when having a large dataset. In following, I have provided a better way.

for i in df['gender']:
    if i == 'M':
        df['group_gender'] = 'dp_m'
            else: 
        df['group_gender'] = 'dp_f'


The more appropriate way

#creating an instant dataframe
import pandas as pd
d = {'gender':['M','M','F', 'M', 'F','M']}
df = pd.DataFrame(d)

#filling the 'group_gender' column
df.loc[df['gender'] == 'M', 'group_gender'] = 'dp_m'
df.loc[df['gender'] == 'F', 'group_gender'] = 'dp_f'


-You are not filling in experimental_gropuping and returning the column:

group(df['experimental_grouping'])


-What is this group? (Can you provide more details about the first part of your code?)