I have the following data frame
df = pd.DataFrame([['1','aa', 'eee', 'text 1', 'text 1'], ['1', 'aa', 'fff', 'text 2'], ['1', 'aa', 'ggg', 'text 3'],
['2','aa', 'eee', 'text 4', 'text 4'], ['2', 'aa', 'fff', 'text 5'], ['2', 'aa', 'ggg', 'text 6'],
['3','bb', 'hhh', 'text 7', 'text 7'], ['3', 'bb', 'jjj', 'text 8'], ['3', 'bb', 'kkk', 'text 9'],
['3', 'bb', 'mmm', 'text 10'], ['4','bb', 'hhh', 'text 11', 'text 11'], ['4', 'bb', 'jjj', 'text 12'],
['4', 'bb', 'kkk', 'text 13'], ['4', 'bb', 'mmm', 'text 14'], ['5','aa', 'eee', 'text 15', 'text 15'],
['5', 'aa', 'fff', 'text 16'], ['5', 'aa', 'ggg', 'text 17']], columns=['foo', 'bar','name_input','string', 'Feature 1'])
Now, I need to add a new column based on three conditions and value must be used from two rows.
The conditions to follow is
- if bar = aa and name_input = fff and name_input = ggg , then new value in column should be text 2 + text 3
In the end, I am aiming to have my output as following,
df = pd.DataFrame([['1', 'aa', 'eee', 'text 1', 'text 1', 'text 2 + text 3'], ['1', 'aa', 'fff', 'text 2'], ['1', 'aa', 'ggg', 'text 3'],
['2', 'aa', 'eee', 'text 4', 'text 4', 'text 5 + text 6'], ['2', 'aa', 'fff', 'text 5'], ['2', 'aa', 'ggg', 'text 6'],
['3', 'bb', 'hhh', 'text 7', 'text 7', 'text 8 + text 9'], ['3', 'bb', 'jjj', 'text 8'], ['3', 'bb', 'kkk', 'text 9'],
['3', 'bb', 'mmm', 'text 10'], ['4', 'bb', 'hhh', 'text 11', 'text 11', 'text 12 + text 13'], ['4', 'bb', 'jjj', 'text 12'],
['4', 'bb', 'kkk', 'text 13'], ['4', 'bb', 'mmm', 'text 14'], ['5','aa', 'eee', 'text 15', 'text 15', 'text 16 + text 17'],
['5', 'aa', 'fff', 'text 16'], ['5', 'aa', 'ggg', 'text 17']], columns=['foo', 'bar', 'name_input', 'string', 'Feature 1', 'Feature 2'])
I tried using the below:
df_merge1.loc[(df_merge1['bar'] == 'aa') & (df_merge1['name_input'] == 'fff') & (df_merge1['name_input'] == 'ggg'), 'Feature 2'] = df_merge1['string'].values[1] + df_merge1['string'].values[2]
However, I am not able to populate the values from the string column to the new column. Seems like im missing something.
Any help is much appreciated!