我有一个.csv文件想要在 FastAPI 应用程序中呈现。我只设法.csv以 JSON 格式呈现文件,如下所示:
def transform_question_format(csv_file_name):
json_file_name = f"{csv_file_name[:-4]}.json"
# transforms the csv file into json file
pd.read_csv(csv_file_name ,sep=",").to_json(json_file_name)
with open(json_file_name, "r") as f:
json_data = json.load(f)
return json_data
@app.get("/questions")
def load_questions():
question_json = transform_question_format(question_csv_filename)
return question_json
Run Code Online (Sandbox Code Playgroud)
当我尝试直接返回时pd.read_csv(csv_file_name ,sep=",").to_json(json_file_name),它起作用了,因为它返回一个字符串。
我应该如何进行?我相信这不是一个好方法。
我有一个包含先验异常值的数据帧。我想至少从“降雨”变量中删除异常值。我按如下方式进行。它看起来有效,但我在第二个图中仍然有异常值。正常吗?
rainfall = df["Rainfall"]
q3 = np.quantile(rainfall, 0.75)
q1 = np.quantile(rainfall, 0.25)
iqr = q3 - q1
upper_bound = q1 + 1.5 * iqr
lower_bound = q3 - 1.5 * iqr
rainfall_wo_outliers = df[(rainfall <= lower_bound) | (rainfall >= upper_bound)]["Rainfall"]
Run Code Online (Sandbox Code Playgroud)
Ps:我之前已经缩放过数据MinMaxScaler