在 pandas 中使用iterrows进行循环 [英] for loop using iterrows in pandas

查看:266
本文介绍了在 pandas 中使用iterrows进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据框,如下所示:

I have 2 dataframes as follows:

data1看起来像这样:

data1 looks like this:

id          address       
1          11123451
2          78947591

data2如下所示:

data2 looks like the following:

lowerbound_address   upperbound_address    place
78392888                 89000000            X
10000000                 20000000            Y

我想在data1中创建另一个列,称为地方",其中包含ID所来自的地方. 例如,在上述情况下, 对于ID 1,我希望Place列包含Y,对于ID 2,我希望Place列包含X. 来自同一位置的ID将会很多.某些ID没有匹配项.

I want to create another column in data1 called "place" which contains the place the id is from. For example, in the above case, for id 1, I want the place column to contain Y and for id 2, I want the place column to contain X. There will be many ids coming from the same place. And some ids don't have a match.

我正在尝试使用以下代码来做到这一点.

I am trying to do it using the following piece of code.

places = []
    for index, row in data1.iterrows():
        for idx, r in data2.iterrows():
            if r['lowerbound_address'] <= row['address'] <= r['upperbound_address']:
                places.append(r['place'])

这里的地址是浮点值.

运行这段代码需要花费很多时间.这让我想知道我的代码是否正确,或者是否有更快的执行方法.

It's taking forever to run this piece of code. It makes me wonder if my code is correct or if there's a faster way of executing the same.

任何帮助将不胜感激. 谢谢!

Any help will be much appreciated. Thank you!

推荐答案

您可以先将cross drop 删除不必要的列:

You can use first cross join with merge and then filter values by boolean indexing. Last remove unecessary columns by drop:

data1['tmp'] = 1
data2['tmp'] = 1
df = pd.merge(data1, data2, on='tmp', how='outer')
df = df[(df.lowerbound_address <= df.address) & (df.upperbound_address >= df.address)]
df = df.drop(['lowerbound_address','upperbound_address', 'tmp'], axis=1)
print (df)
   id   address place
1   1  11123451     Y
2   2  78947591     X

使用 itertuples ,最后创建 DataFrame.from_records :

places = []
for row1 in data1.itertuples():
    for row2 in data2.itertuples():
        #print (row1.address)
        if (row2.lowerbound_address <= row1.address <= row2.upperbound_address):
            places.append((row1.id, row1.address, row2.place))    
print (places)
[(1, 11123451, 'Y'), (2, 78947591, 'X')]

df = pd.DataFrame.from_records(places)
df.columns=['id','address','place']
print (df)
   id   address place
0   1  11123451     Y
1   2  78947591     X

使用 apply :

Another solution with apply:

def f(x):
    for row2 in data2.itertuples():
        if (row2.lowerbound_address <= x <= row2.upperbound_address):
            return pd.Series([x, row2.place], index=['address','place'])

df = data1.set_index('id')['address'].apply(f).reset_index()
print (df)
   id   address place
0   1  11123451     Y
1   2  78947591     X

时间:

N = 1000:

如果saome值不在范围内,则忽略溶液bc.检查df1的最后一行.

If saome values are not in range, in solution b and c are omited. Check last row of df1.

In [73]: %timeit (data1.set_index('id')['address'].apply(f).reset_index())
1 loop, best of 3: 2.06 s per loop

In [74]: %timeit (a(df1a, df2a))
1 loop, best of 3: 82.2 ms per loop

In [75]: %timeit (b(df1b, df2b))
1 loop, best of 3: 3.17 s per loop

In [76]: %timeit (c(df1c, df2c))
100 loops, best of 3: 2.71 ms per loop

计时代码:

np.random.seed(123)
N = 1000
data1 = pd.DataFrame({'id':np.arange(1,N+1), 
                   'address': np.random.randint(N*10, size=N)}, columns=['id','address'])

#add last row with value out of range
data1.loc[data1.index[-1]+1, ['id','address']] = [data1.index[-1]+1, -1]
data1 = data1.astype(int)
print (data1.tail())

data2 = pd.DataFrame({'lowerbound_address':np.arange(1, N*10,10), 
                      'upperbound_address':np.arange(10,N*10+10, 10),
                      'place': np.random.randint(40, size=N)})

print (data2.tail())
df1a, df1b, df1c = data1.copy(),data1.copy(),data1.copy()
df2a, df2b ,df2c = data2.copy(),data2.copy(),data2.copy()


def a(data1, data2):
    data1['tmp'] = 1
    data2['tmp'] = 1
    df = pd.merge(data1, data2, on='tmp', how='outer')
    df = df[(df.lowerbound_address <= df.address) & (df.upperbound_address >= df.address)]
    df = df.drop(['lowerbound_address','upperbound_address', 'tmp'], axis=1)
    return (df)


def b(data1, data2):
    places = []
    for row1 in data1.itertuples():
        for row2 in data2.itertuples():
            #print (row1.address)
            if (row2.lowerbound_address <= row1.address <= row2.upperbound_address):
                places.append((row1.id, row1.address, row2.place))    

        df = pd.DataFrame.from_records(places)
        df.columns=['id','address','place']

    return (df)


def f(x):
    #use for ... else for add NaN to values out of range
    #http://stackoverflow.com/q/9979970/2901002
    for row2 in data2.itertuples():
        if (row2.lowerbound_address <= x <= row2.upperbound_address):
             return pd.Series([x, row2.place], index=['address','place'])
    else:
        return pd.Series([x, np.nan], index=['address','place'])


def c(data1,data2):
    data1 = data1.sort_values('address')
    data2 = data2.sort_values('lowerbound_address')
    df = pd.merge_asof(data1, data2, left_on='address', right_on='lowerbound_address')
    df = df.drop(['lowerbound_address','upperbound_address'], axis=1)
    return df.sort_values('id')


print (data1.set_index('id')['address'].apply(f).reset_index())
print (a(df1a, df2a))
print (b(df1b, df2b))
print (c(df1c, df2c))

仅使用 merge_asof 的解决方案c 在大型DataFrame上效果很好:

Only solution c with merge_asof works very nice with large DataFrame:

N=1M:

In [84]: %timeit (c(df1c, df2c))
1 loop, best of 3: 525 ms per loop

有关在文档中合并asof的更多信息.

这篇关于在 pandas 中使用iterrows进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆