假设我有以下 Dataframe :
base_df = pd.DataFrame({'animal':['dog', 'cat'], 'noise':['bark', 'meow']}, index=[1,2])
new_info_df = pd.DataFrame({'animal':['wolf', 'sheep'], 'noise':['woof', 'baa']}, index=[1,3])
字符串
我想创建一个新的dataframe,其中包含base_df中的所有数据和new_info_df中的行,其中行的索引尚未出现在base_df中,即更新的dataframe应该等价于:
updated_df = pd.DataFrame({'animal':['dog', 'cat', 'sheep'], 'noise':['bark', 'meow', 'baa']}, index=[1,2,3])
型
我试过了
pd.concat([base_df, new_info_df], join='outer', axis=1)
型
但我最终与列‘动物‘和‘噪音‘被重复,我不想。
2条答案
按热度按时间bttbmeg01#
你可以尝试boolean索引+
.concat
:字符串
图纸:
型
des4xlb02#
要创建一个包含所需列('animal'和'noise')的新数据框架,将base_df中的数据和new_info_df中的行的索引合并在一起,可以执行以下步骤:
字符串
输出将为:
型