1

だから私はこのprep_dat関数を持っていて、次のcsvデータを与えています:

identifier,Hugo_Symbol,Tumor_Sample_Barcode,Variant_Classification,patient
1,patient,a,Silent,6
22,mutated,d,e,7
1,Hugo_Symbol,f,g,88

この prep_data 関数内には、この行があります

  gene_mutation_df.index.set_names(['Hugo_Symbol', 'patient'], inplace=True)

ただし、という行になるとエラーが発生し続けます

ValueError: Length of new names must be 1, got 2

行に何か問題がありますか、それとも機能に何か問題がありますか

ソースコード全体はこちら

import pandas as pd
import numpy as np
PRIMARY_TUMOR_PATIENT_ID_REGEX = '^.{4}-.{2}-.{4}-01.*'
SHORTEN_PATIENT_REGEX = '^(.{4}-.{2}-.{4}).*'

def mutations_for_gene(df):
  mutated_patients = df['identifier'].unique()
  return pd.DataFrame({'mutated': np.ones(len(mutated_patients))}, index=mutated_patients)
  
def prep_data(mutation_path):
  df = pd.read_csv(mutation_path, low_memory=True, dtype=str, header = 0)#Line 24 reads in a line memory csv file from the given path and parses it based on '\t' delimators, and casts the data to str
  
  df = df[~df['Hugo_Symbol'].str.contains('Hugo_Symbol')] #analyzes the 'Hugo_Symbol' heading within the data and makes a new dataframe where any row that contains 'Hugo_Symbol' is dropped

  df['Hugo_Symbol'] = '\'' + df['Hugo_Symbol'].astype(str) # Appends ''\'' to all the data remaining in that column
  
  df['Tumor_Sample_Barcode'] = df['Tumor_Sample_Barcode'].str.strip() #strips away whitespace from the data within this heading
  non_silent = df.where(df['Variant_Classification'] != 'Silent') #creates a new dataframe where the data within the column 'Variant_Classification' is not equal to 'Silent'

  df = non_silent.dropna(subset=['Variant_Classification']) #Drops all the rows that are missing at least one element
  
  non_01_barcodes = df[~df['Tumor_Sample_Barcode'].str.contains(PRIMARY_TUMOR_PATIENT_ID_REGEX)]['Tumor_Sample_Barcode'] #Creates a new dataframe of all the data within the 'Tumor_Sample_Barcode' column that does not match the PRIMARY_TUMOR_PATIENT_ID_REGEX
  #TODO: Double check that the extra ['Tumor_Sample_Barcode'] serves no purpose
  df = df.drop(non_01_barcodes.index)
  print(df)
  shortened_patients = df['Tumor_Sample_Barcode'].str.extract(SHORTEN_PATIENT_REGEX, expand=False)
  df['identifier'] = shortened_patients
  
  gene_mutation_df = df.groupby(['Hugo_Symbol']).apply(mutations_for_gene)
  gene_mutation_df.index.set_names(['Hugo_Symbol', 'patient'], inplace=True)
  gene_mutation_df = gene_mutation_df.reset_index()
  gene_patient_mutations = gene_mutation_df.pivot(index='Hugo_Symbol', columns='patient', values='mutated')
  
  return gene_patient_mutations.transpose().fillna(0)

どんな助けでも大歓迎です(私はこれが具体的ではないことを知っています、私はまだこの関数が正確に何をするのか、そしてそれをテストするためのデータをどのように作ることができるのかを理解しようとしています)

4

0 に答える 0