Pandas的SettingWithCopyWarning报警

    科技2024-10-04  23

    fpath = "datas/beijing_tianqi/beijing_tianqi_2018.csv" df = pd.read_csv(fpath) df.loc[:,"bWendu"] = df["bWendu"].str.replace("℃","").astype('int32') df.loc[:,"yWendu"] = df["yWendu"].str.replace("℃","").astype('int32') # print(df.head()) # 1、复现 condition = df["ymd"].str.startswith("2018-03") # df[condition]["wen_cha"] = df["bWendu"]-df["yWendu"] # 导致报错 # print(df[condition].head()) # 解决方法1 # df.loc[condition,"wen_cha"] = df["bWendu"] - df["yWendu"] # print(df[condition].head()) # 解决方法2 df_month3 = df[condition].copy() # print(df_month3.head()) df_month3["wen_cha"] = df["bWendu"]-df["yWendu"] print(df_month3.head())
    Processed: 0.010, SQL: 8