【发布时间】:2019-07-29 22:03:01
【问题描述】:
我想创建一个可以应用于 df 列的函数,该函数将识别该列('C2017Value')中位于范围列表(范围)中的任何范围内的所有条目......并输出范围内的相应条目及其 c 值到结果字典 {'c' : C2017Value},如下所示:
results = {'c3': 268} #268 is within one of the ranges
我被困在代码上,如果有任何见解和反馈,我将不胜感激。
df #dataframe with two columns, 'c' and C2017Value
'c1', 137674167
'c2', 2166178
'c3', 268
ranges = [
(261, 4760),
(12273391, 11104571063),
(45695385, 4134339925),
(15266178, 1376748162),
(10106104, 97810284),
(6492248, 588025190)
]
这是我对这个功能的尝试:
between_range = [c2017 for c2017
in sorted(ranges)
if ranges[0] <= value <= ranges[1]
][0]
def get_output_list(c2017value):
output_list = []
index = 0
for c in df:
if ranges[0][0] <= c2017value <= ranges[0][1]:
output_list.append(c)
else:
index += 1
return output_list
def get_output_list0(df, ranges):
output_list = []
index = 0
for c in df:
if c.column_value('C2017Value') == xrange() ranges[index]:
output_list.append(c)
else:
index += 1
return output_list
def get_output_list1(C2017Value):
for x, y in sorted(ranges):
if any(x <= C2017Value < y):
for c in ms_df:
output.append(c)
def get_output_list2(CValue):
output = []
ranges = get_ranges()
for c in ms_df:
##if MINvalue<= CValue <=MAXvalue:
if C2017Value in ranges(MINvalue, MAXvalue):
return c
output.append(c)
break
def get_output_list3(C2017Value):
##ranges = get_ranges()
for c in df:
##if MINvalue<= CValue <=MAXvalue:
if CValue in ranges:
return c
def get_output_list4(df, C2017Value, ranges[0:1]):
##ranges = get_ranges()
for c in df_countries:
##if MINvalue<= CValue <=MAXvalue:
if C2017Value in ranges:
#if C2017Value in range(ranges):
#return c
output.append(c)
return output
def get_output_list5(C2017Value:
for c in df_countries:
for x in sorted(ranges):
range_list = ranges[range_name]
if any(start <= number < end for start,end):
results.setdefault(range_name, 0) += 1
def get_output_list6(C2017Value):
for c in ms_df:
for x, y in sorted(ranges):
if any(x <= C2017Value < y):
output.append(c)
这两个可能是最有希望的尝试:
between_range = [c2017 for c2017
in sorted(ranges)
if ranges[0] <= value <= ranges[1]
][0]
def get_output_list(c2017value):
output_list = []
index = 0
for c in df:
if ranges[0][0] <= c2017value <= ranges[0][1]:
output_list.append(c)
else:
index += 1
return output_list
between_range 收到以下错误消息: "
【问题讨论】:
-
如果你得到
<= not supported between instances of 'int' and 'str'",那么你应该检查你比较的——也许你比较261 <= 'c3'而不是261 <= 268 -
df[ df['C2017Value'].between(261, 4760) ]怎么样?它给出了在261, 4760范围内具有'C2017Value'的所有行 -
是的,这是单个范围的绝佳解决方案...我想获取范围列表中任何范围内的所有 ['C2017Value'] 列值。
-
您可以在
for a,b in ranges中运行between(a, b),但它会多次检查DF 中的所有行,因此对于大DF 会产生问题
标签: python pandas iterator range