import pandas as pd
data = { 'survey_id': [1, 1, 1, 2, 2, 2], 'survey_question': ['osat score', None, None, 'osat score', None, None], 'survey_score': [5, None, None, 7, None, None] }
df = pd.DataFrame(data)
osat_scores = df[df['survey_question'] == 'osat score'].set_index('survey_id')['survey_score'].to_dict()
df.loc[df['survey_question'].isnull() & df['survey_score'].isnull(), 'survey_question'] = 'osat score' df.loc[df['survey_score'].isnull(), 'survey_score'] = df['survey_id'].map(osat_scores)
import ace_tools as tools; tools.display_dataframe_to_user(name="Updated Survey Data", dataframe=df)