我正在尝试合并到熊猫数据帧,尽管我想要的实际上可能不是合并。
我在两个框架中有两列相匹配,其中一列共享可用于连接的唯一值。另一列有一个空字段,一个填充了一个字段。
我想覆盖空字段,同时在唯一字段上进行匹配,但只保留被覆盖的列,我不希望第二个DataFrame中的其余列。
希望以下内容会进一步解释
>>> animals = [{"animal" : "dog", "name" : "freddy", "food" : ""},{"animal" : "cat", "name" : "dexter", "food" : ""},{"animal" : "dog", "name" : "lou lou", "food" : ""}]
>>> foods = [{"name" : "freddy", "food" : "dog mix", "brand" : "doggys dog"},{"name" : "dexter", "food" : "fussy cat mix", "brand" : "fish fishy"},{"name" : "lou lou", "food" : "bones", "brand" : "i was a cow"}]
>>> a_pd = pd.DataFrame(animals)
>>> a_pd
animal food name
0 dog freddy
1 cat dexter
2 dog lou lou
>>> f_pd = pd.DataFrame(foods)
>>> f_pd
brand food name
0 doggys dog dog mix freddy
1 fish fishy fussy cat mix dexter
2 i was a cow bones lou lou
>>>
>>>
>>> animal_data = a_pd.merge(f_pd, on='name', how='left')
>>> animal_data
animal food_x name brand food_y
0 dog freddy doggys dog dog mix
1 cat dexter fish fishy fussy cat mix
2 dog lou lou i was a cow bones
>>>
我应该只吃食物,而不想要这个品牌(还要注意,这是示例数据,实时数据有很多列
所需结果
>>> animal_data
animal name food
0 dog freddy dog mix
1 cat dexter fussy cat mix
2 dog lou lou bones
答案 0 :(得分:4)
使用:
animal_data = a_pd.merge(f_pd, on='name', how='left', suffixes=('_x','')).drop('food_x', axis=1)
输出:
animal name brand food
0 dog freddy doggys dog dog mix
1 cat dexter fish fishy fussy cat mix
2 dog lou lou i was a cow bones
或
a_pd[['animal','name']].merge(f_pd, how='left')
输出:
animal name brand food
0 dog freddy doggys dog dog mix
1 cat dexter fish fishy fussy cat mix
2 dog lou lou i was a cow bones
答案 1 :(得分:3)
您可以使用update
a_pd.set_index('name',inplace=True)
a_pd.update(f_pd.set_index('name'))
a_pd
Out[68]:
animal food
name
freddy dog dog mix
dexter cat fussy cat mix
lou lou dog bones
a_pd.reset_index()
Out[69]:
name animal food
0 freddy dog dog mix
1 dexter cat fussy cat mix
2 lou lou dog bones
或者我们使用map
a_pd.food=a_pd.name.map(f_pd.set_index('name').food)
a_pd
Out[74]:
animal food name
0 dog dog mix freddy
1 cat fussy cat mix dexter
2 dog bones lou lou
答案 2 :(得分:2)
我会尝试drop
或只是选择要保留的列:
animal_data.drop(['food_x', 'brand'], axis=1, inplace=True)
或
animal_data = animal_data[['animal', 'name', 'food']]
答案 3 :(得分:2)
最好合并数据框的视图,这些视图不包含合并数据框中不需要的列。例如:
point
这可能会更快,并且如果处理非常大的数据帧,可能会节省一些内存,因为在合并中仅结转了相关列。