【发布时间】:2020-12-29 05:31:06
【问题描述】:
我是 python 新手,我目前正在做一个项目,我必须合并两个数据框。一个称为cancer_df 的数据框是按县、年、性别、性别等划分的癌症发病率。另一个称为hspa_df 的数据框是按县和年划分的健康评分(仅供参考,它只是加利福尼亚州的县)。我想将我的两个关于县和年的数据框结合起来。 Here is the cancer dataframe before the merge 和 Here is the hspa dataframe before the merge
然后我导入了我的数据并尝试了以下合并:
merged_df= pd.merge(cancer_df, hspa_df, on="County" , how="outer")
但是,这似乎是追加数据而不是合并它。它在最后添加了我的 hspa_df 并填充了它们作为 NaN 共享的变量的顶部。为什么会这样?我已经成功地将此合并与其他数据框一起使用,但我将它们合并到数字列上,而不是字符串上。 Here is the merged dataframes head 和 Here is the merged dataframes tail
【问题讨论】:
-
您需要将
how更改为inner才能使用两个帧中的关键点的交集。 -
当我这样做时,它只保留一个县。但是,他们共享 46 个县(每年)。