【问题标题】:Better way to replace values in DataFrame from large dictionary从大字典中替换 DataFrame 中的值的更好方法
【发布时间】:2017-03-24 13:00:57
【问题描述】:

我已经编写了一些代码,它使用字典将 DataFrame 中的值替换为另一个框架中的值,并且它正在工作,但我在一些大文件上使用它,其中字典可能会变得很长。几千双。然后,当我使用此代码时,它的运行速度非常慢,并且在某些情况下它也出现了内存不足的情况。

我有点相信我的方法远非最佳,必须有一些更快的方法来做到这一点。我创建了一个简单的示例,可以满足我的需求,但是对于大量数据来说这很慢。希望有人有更简单的方法来做到这一点。

import pandas as pd

#Frame with data where I want to replace the 'id' with the name from df2
df1 = pd.DataFrame({'id' : [1, 2, 3, 4, 5, 3, 5, 9], 'values' : [12, 32, 42,    51, 23, 14, 111, 134]})

#Frame containing names linked to ids
df2 = pd.DataFrame({'id' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'name' : ['id1',   'id2', 'id3', 'id4', 'id5', 'id6', 'id7', 'id8', 'id9', 'id10']})

#My current "slow" way of doing this.

#Starts by creating a dictionary from df2
#Need to create dictionaries from the domain and banners tables to link ids
df2_dict = dict(zip(df2['id'], df2['name']))

#and then uses the dict to replace the ids with name in df1
df1.replace({'id' : df2_dict}, inplace=True)

【问题讨论】:

    标签: python-3.x pandas dictionary replace


    【解决方案1】:

    我认为您可以使用 mapSeries 转换后的 to_dict - 如果 df2 中不存在值,则获取 NaN

    df1['id'] = df1.id.map(df2.set_index('id')['name'].to_dict())
    print (df1)
        id  values
    0  id1      12
    1  id2      32
    2  id3      42
    3  id4      51
    4  id5      23
    5  id3      14
    6  id5     111
    7  id9     134
    

    replace,如果df2 中不存在值,则让df1 中的原始值:

    df1['id'] = df1.id.replace(df2.set_index('id')['name'])
    print (df1)
        id  values
    0  id1      12
    1  id2      32
    2  id3      42
    3  id4      51
    4  id5      23
    5  id3      14
    6  id5     111
    7  id9     134
    

    示例:

    #Frame with data where I want to replace the 'id' with the name from df2
    df1 = pd.DataFrame({'id' : [1, 2, 3, 4, 5, 3, 5, 9], 'values' : [12, 32, 42,    51, 23, 14, 111, 134]})
    print (df1)
    #Frame containing names linked to ids
    df2 = pd.DataFrame({'id' : [1, 2, 3, 4, 6, 7, 8, 9, 10], 'name' : ['id1',   'id2', 'id3', 'id4', 'id6', 'id7', 'id8', 'id9', 'id10']})
    print (df2)
    
    df1['new_map'] = df1.id.map(df2.set_index('id')['name'].to_dict())
    df1['new_replace'] = df1.id.replace(df2.set_index('id')['name'])
    print (df1)
       id  values new_map new_replace
    0   1      12     id1         id1
    1   2      32     id2         id2
    2   3      42     id3         id3
    3   4      51     id4         id4
    4   5      23     NaN           5
    5   3      14     id3         id3
    6   5     111     NaN           5
    7   9     134     id9         id9
    

    【讨论】:

    • 这似乎有效。但是有没有办法将“值”列保留在 df1.我似乎无法弄清楚如何编写这个来更改 id 列并保留 values 列。 NVM,刚刚想通了。可以这样做: df1['id'].replace(df2.set_index('id')['name'], inplace=True)
    • 对不起,我没有添加分配,请参阅更新我的答案。
    猜你喜欢
    • 2021-10-16
    • 2019-07-25
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2020-10-05
    相关资源
    最近更新 更多