簡體   English   中英

一起迭代字典和數據框的更快方法?

[英]Faster way to iterate over dictionary and dataframe together?

我有一個字典和一個具有相同鍵/列的 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

試試這個,它應該給你你想要的。

# 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM