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表示日本。怎么做?
我尝试了以下三个代码,都失败了:
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 :(得分:0)
您可以使用此代码段完成您要实现的目标。由于您要与两列进行比较:upper_bound和lower_bound并且两者都具有相同的索引,因此一次一行地迭代而不是一次遍历整列是合乎逻辑的。
require 'capistrano/bundler'