我在将一个数据帧附加到另一个数据帧时遇到问题。数据框“ dataframe_sbp_scores”不为空。这是它的一个片段:
cik nextCik searchFrac group
138988 1800 200406 0.026923 1
138989 1800 78003 0.026683 1
138990 1800 14272 0.020913 1
138991 1800 10456 0.018990 1
138992 1800 5187 0.014663 1
138993 1800 310158 0.013702 1
138994 1800 59478 0.013702 1
138995 1800 318154 0.013462 1
当我尝试附加到“ df_final”时,它只是返回一个空的数据帧,但没有发生错误。有人知道这是什么解释吗?下面是我的代码:
for year in range(2004, 2017):
df_final = pd.DataFrame()
year_base = year
year_peers = year - 1
bases_list = dataframe_bases[f"{year_base}"].tolist()
counter = 0
for base in tqdm(bases_list):
counter += 1
dataframe_sbp_scores = pd.read_csv(path_to_csv + f"S&P1500 adjusted 95% {year_peers}final.csv")
dataframe_sbp_scores = dataframe_sbp_scores[dataframe_sbp_scores["cik"] == base]
dataframe_sbp_scores = dataframe_sbp_scores.head(20)
dataframe_sbp_scores["group"] = counter
print(dataframe_sbp_scores)
df_final.append(dataframe_sbp_scores)
print(df_final)
if counter == 10:
df_final.to_csv(path_save_groups + f"peer_groups_{year_base}.csv", index=False)
break
if counter == 10:
break
答案 0 :(得分:2)
该追加未就地发生。试试:
df_final = df_final.append(dataframe_sbp_scores)