2017-01-26 19 views
0
df1: 
lower_bound_ip_address   upper_bound_ip_address country 
0    16777216.0    16777471   Australia 
1    16777472.0    16777727   China 
2    16777728.0    16778239   China 
3    16778240.0    16779263   Australia 
4    16779264.0    16781311   China 

df: 
    ip_address 
0 7.327584e+08 
1 3.503114e+08 
2 2.621474e+09 
3 3.840542e+09 
4 4.155831e+08 
5 2.809315e+09 
6 3.987484e+09 
7 1.692459e+09 
8 3.719094e+09 
9 3.416747e+08 

df ['ip_address']とdf1 ['country']を一致させたいとします。特定のip_addressの範囲は特定の国に対応しています。例:729808896-734003199は日本を示します。どうやってするか?1つのテーブルにキーがある場合は2つのテーブルをマージし、もう1つのテーブルにキーの範囲しかない場合

for x in df['ip_address']: 
    if x<=df1['upper_bound_ip_address'] and x>=df1['lower_bound_ip_address']: 
     df['country']=df1['country'] 

TypeError: len() of unsized object 


for x in df['ip_address']: 
    for y in df1: 
    if x<=y['upper_bound_ip_address'] and x>=y['lower_bound_ip_address']: 
     x['country']=y['country'] 

TypeError: string indices must be integers 



for x in range(0, len(df)): 
    for y in range(0, len(df1)): 
    if (df.iloc[x,'ip_address'] <= df1.iloc[y,'upper_bound_ip_address'] and (df.iloc[x,'ip_address'] >= df1.iloc[y,'lower_bound_ip_address']): 
     df['country']=df1.iloc[y,'country'] 

SyntaxError: invalid syntax 

答えて

0

あなたはこのスニペットを使用して行うことができます達成しようとしているもの:

は、私はすべてが失敗し、次の三つのコードを試してみました。 upper_boundとlower_boundの2つの列を比較したいが、どちらも同じインデックスを持っているので、列全体ではなく行ごとに一度に反復することは論理的です。

for i, x in df.iterrows(): 
    for index, row in df1.iterrows(): 
     if x['ip_address']<=row['upper_bound_ip_address'] and x['ip_address']>=row['lower_bound_ip_address']: 
      df.ix[i, 'country']=row['country'] 
関連する問題