首页 > 解决方案 > 一起迭代字典和数据框的更快方法?

问题描述

我有一个字典和一个具有相同键/列的 DataFrame。然而,DataFrame 缺少一些数据,我将使用字典填充这些数据。这是一个最小的例子,我的数据集要大得多。

mydict = {'one': ['foo', 'bar'], 'two': ['foo', 'bar']}
mydf = pd.DataFrame({'one': ['N/A', 'bar'], 'two': ['foo', 'N/A'], 'foo': ['foo', 'bar'], 'bar': ['foo', 'bar']})

def myfunc(mydict):
    for i,k in mydict.items():
            for m in k:
                mydf[i].replace(to_replace='N/A', value=mydf[m], inplace=True)


for f,g in mydf.iterrows():
        for h in g:
            if h != 'N/A':
                myfunc(mydict)

for i,v in mydict.items(): 
    mydf.drop(columns=v, inplace=True, errors='ignore')

当我在更大的数据集上运行我的函数时,内核不会停止运行。什么是更快的方法来做到这一点?我想尝试使用 df.apply() 或矢量化功能,但不知道如何。上面示例的输出如下所示:

    one two
0   foo foo
1   bar bar

标签: pythonpandasloopsdictionaryiteration

解决方案


试试这个,它应该给你你想要的。

# Fill the values using your dictionary
for k, v in mydict.items():
    mydf[k] = v  

# Drop the columns you don't want
mydf.drop(columns=['foo','bar'], inplace=True)  

你会得到这个:

    one two
0   foo foo
1   bar bar

推荐阅读