【解決方法】リストをタプルに変換して解決する一般的な解決策 "ハッシュ不可能な型: リスト" 私のコードで動作していません


ユーザーからデータフレームの列名を取得し、その列の空の値をユーザーが指定した値で埋める関数があります。 Google Colab でコードを実行しましたが、正常に動作しています。 しかし、API を介して空の辞書を返しています。 コードを確認して、エラーの場所をお知らせください。

私が試したこと:

欠損値コード

def fill_num_random_fun(df, col_inputs):
   
   col_name=col_inputs[0]
   col_value=col_inputs[1]
 
   df[col_name]=df[col_name].fillna(value=col_value, inplace=True)
   
    #print("this is col_name",col_name)
    #print("this is col_value",col_value)
    #print("this is col_inputs",col_inputs)

   result_dict = df.to_dict(orient='records')
   for record in result_dict:
        for key, value in record.items():
            if isinstance(value, float):
                record[key] = str(value)
   return result_dict


def cleaning_and_analysis(csv_filepath, operation=None, raw_data=None):
     csv_dataframe = pd.read_csv(csv_filepath) 
     result={}
     if raw_data is not None and isinstance(raw_data, list):

        if operation == 'fillWithDesiredValue':
            fill_with_desired_input = raw_data
            result10 = fill_num_random_fun(csv_dataframe, fill_with_desired_input)
            #print(result10)
            if not result:
               result[operation] = [({fill_with_desired_input: result10})]
            else:
               result[operation].append({fill_with_desired_input: result10})

高速 API

import json
from typing import Union
from typing import Optional
import uvicorn
import re

from fastapi import FastAPI
from pydantic import BaseModel
from Data_Cleaning import cleaning_and_analysis
from typing import Any, Optional


app = FastAPI()

class Item(BaseModel):
    filepath: str
    operation: Optional[str] = None
    operand: Optional[list] = None

@app.post("/replacement_route")
async def data_cleaning(item: Item):
    filepath = item.filepath
    operation_name = item.operation
    operands = item.operand
       
    cleaned_data = {} # initialize cleaned_data to an empty dictionary
    if len(operands) >=1:
       
        for operand in operands:
            if isinstance(operand, dict): # check if 'operand is a dictionary'
              for operation_name,operand_values in operand.items():
                    #print(operation_name)    
                    #print(operand_values)
                    cleaned_data.update(cleaning_and_analysis(filepath, operation=operation_name, raw_data=operand_values))
                    
    return cleaned_data


if __name__ == "__main__": 
     uvicorn.run(app, host="0.0.0.0", port=8000)

郵便配達ペイロード

{
    "filepath": "C:/kidney_disease.csv",
    "operand": [{"fillWithDesiredValue": ["sod",999]}]
        
}

出力

result[operation] = [({fill_with_desired_input: result10})]
TypeError: unhashable type: 'list'

Postman メッセージ: 内部サーバー エラー

私のデバッグ結果

API で

print(operation_name) => fillWithDesiredValue
print(operand_values) =>  ['sod', 999]

コード内

print("this is col_name",col_name)    =>  this is col_name sod
   print("this is col_value",col_value)  =>  this is col_value 999
   print("this is col_inputs",col_inputs) => this is col_inputs ['sod', 999]



   print(result10) ==> [{'id': 0, 'age': '48.0', 'bp': '80.0', 'sg': '1.02', 'al': '1.0', 'su': '0.0', 'rbc': 'nan', 'pc': 'normal', 'pcc': 'notpresent', 'ba': 'notpresent', 'bgr': '121.0', 'bu': '36.0', 'sc': '1.2', 'sod': None, 'pot': 'nan', 'hemo': '15.4', 'pcv': '44', 'wc': '7800', 'rc': '5.2', 'htn': 'yes', 'dm': 'yes', 'cad': 'no', 'appet': 'good', 'pe': 'no', 'ane': 'no', 'classification': 'ckd'}, {'id': 1, 'age': '7.0', 'bp': '50.0', 'sg': '1.02', 'al': '4.0', 'su': '0.0', 'rbc': 'nan', 'pc': 'normal', 'pcc': 'notpresent', 'ba': 'notpresent', 'bgr': 'nan', 'bu': '18.0', 'sc': '0.8', 'sod': None, 'pot': 'nan', 'hemo': '11.3', 'pcv': '38', 'wc': '6000', 'rc': 'nan', 'htn': 'no', 'dm': 'no', 'cad': 'no', 'appet': 'good', 'pe': 'no', 'ane': 'no', 'classification': 'ckd'}, {'id': 2, 'age': '62.0', 'bp': '80.0', 'sg': '1.01', 'al': '2.0', 'su': '3.0', 'rbc': 'normal', 'pc': 'normal', 'pcc': 'notpresent', 'ba': 'notpresent', 'bgr': '423.0', 'bu': '53.0', 'sc': '1.8', 'sod': None, 'pot': 'nan', 'hemo': '9.6', 'pcv': '31', 'wc': '7500', 'rc': 'nan', 'htn': 'no', 'dm': 'yes', 'cad': 'no', 'appet': 'poor', 'pe': 'no', 'ane': 'yes', 'classification': 'ckd'}..........]

ディクショナリ アイテムは適切にキャプチャされています。 これらが Postman によってキャプチャされないのはなぜですか? タプルの変換が間違っていましたか?

コメント

タイトルとURLをコピーしました