Preprocess df and generate final dict
(df: pd.DataFrame, prefix: str = '')
| 33 | return res |
| 34 | |
| 35 | def preprocess_question(df: pd.DataFrame, prefix: str = ''): |
| 36 | ''' |
| 37 | Preprocess df and generate final dict |
| 38 | ''' |
| 39 | res = [] |
| 40 | |
| 41 | # uppercase to lowercase |
| 42 | df.rename(columns={ |
| 43 | 'Question': 'question', |
| 44 | 'Answer': 'answer' |
| 45 | }, inplace=True) |
| 46 | |
| 47 | for idx in range(df.shape[0]): |
| 48 | to_append = { |
| 49 | 'question': df['question'].iloc[idx], |
| 50 | 'options': [], |
| 51 | 'answer': df['answer'].iloc[idx].strip().upper() |
| 52 | } |
| 53 | question = df['question'].iloc[idx] |
| 54 | |
| 55 | query = prefix + '''问题:{question}\n'''.format(question=question) |
| 56 | |
| 57 | for option in ['A', 'B', 'C', 'D']: |
| 58 | if df[option].iloc[idx]: |
| 59 | to_append['options'].append(option) |
| 60 | to_append[option] = df[option].iloc[idx] |
| 61 | to_add = '{}. {}\n'.format(option, df[option].iloc[idx]) |
| 62 | query += to_add |
| 63 | |
| 64 | to_add = '答案:' |
| 65 | query += to_add |
| 66 | to_append['query'] = query |
| 67 | res.append(to_append) |
| 68 | return res |