Help me refine this function. I want a final dataframe with the stated columns. def…
- User
Help me refine this function. I want a final dataframe with the stated columns.
def save_combination_with_metadata(): """ loads gpt and mistral data again loads the metadata file and merges on the testimony id column saves the combined dataframe to a new csv file with the columns: - testimony_id, mistral_belief_structure, mistral_practice_structure, mistral_belief_coverage_level, mistral_practice_coverage_level, gpt_belief_structure, gpt_practice_structure, gpt_belief_coverage_level, gpt_practice_coverage_level
:return: """ meta_df = pd.read_csv("/home/esther/PycharmProjects/religious-paths/data/" "metadata/trajectories_metadata - more_features.csv") gpt_df = load_data("gpt") mistral_df = load_data("mistral") # rename columns to indicate model combined_df = meta_df.set_index('tid').join( gpt_df[['belief_traj', 'practice_traj']].rename(columns={ 'belief_traj': 'gpt_belief_traj', 'practice_traj': 'gpt_practice_traj', 'practice_structure': 'gpt_practice_structure', 'practice_coverage_level': 'gpt_practice_coverage_level', 'belief_structure': 'gpt_belief_structure', 'belief_coverage_level': 'gpt_belief_coverage_level' }) ).join( mistral_df[['belief_traj', 'practice_traj']].rename(columns={ 'belief_traj': 'mistral_belief_traj', 'practice_traj': 'mistral_practice_traj', 'practice_structure': 'mistral_practice_structure', 'practice_coverage_level': 'mistral_practice_coverage_level', 'belief_structure': 'mistral_belief_structure', 'belief_coverage_level': 'mistral_belief_coverage_level' }) ) # filter columns to keep only those with 'structure' or 'coverage_level' in their names columns_to_keep = [col for col in combined_df.columns if 'structure' in col or 'coverage_level' in col or 'tid' in col] columns_to_keep += ['gender', 'year_of_birth', 'country_of_birth'] combined_df = combined_df[columns_to_keep] combined_df.to_csv("data/combined_metadata_taxonomy.csv") - Assistant
- User
The last line throwsd this
KeyError: "['mistral_belief_structure', 'mistral_practice_structure', 'mistral_belief_coverage_level', 'mistral_practice_coverage_level', 'gpt_belief_structure', 'gpt_practice_structure', 'gpt_belief_coverage_level', 'gpt_practice_coverage_level'] not in index"
- Assistant