给定一个范围表(start, end)
:
name blue green yellow purple
a 1, 5 654, 678 11, 15
b 88761, 88776
c 1211, 1215 38, 47
d 89, 95 1567, 1578
和一个数据框df
:
Supplier colour
Abi 1
John 678
Smith 120
Tim 1570
Don 87560
我怎样才能过滤df
仅包含其值在colour
列是否在表中提供的范围内?
并创建第三列,将列中的值关联起来colour
to the row-column
范围表中的名称。我想要决赛df
看起来像这样:
Supplier colour Source
Abi 1 a-blue
John 678 a-yellow
Tim 1570 d-purple
谢谢你!
s = df2.stack()
print(s)
Gives:
a 1, name 5
blue 654,
green 678
yellow 11,
purple 15
b 88761, name 88776
c 1211, name 1215
blue 38,
green 47
d 89, name 95
blue 1567,
green 1578
dtype: object
And:
s = df2.stack()
s = s.str.split("[, ]+").apply(lambda x: pd.Interval(*map(int, x), closed="both"))
print(s)
Gives:
Traceback (most recent call last):
File "/Users/PycharmProjects/sup.py", line 12, in <module>
s = s.str.split("[, ]+").apply(lambda x: pd.Interval(*map(int,x), closed="both"))
File "/Users/.conda/envs/lib/python3.8/site-packages/pandas/core/series.py", line 4200, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/lib.pyx", line 2402, in pandas._libs.lib.map_infer
File "/Users/PycharmProjects/sup.py", line 12, in <lambda>
s = s.str.split("[, ]+").apply(lambda x: pd.Interval(*map(int,x), closed="both"))
TypeError: 'float' object is not iterable