我有一个熊猫数据帧如下:
mail = DataFrame({'mail' : ['adv@gmail.com', 'fhngn@gmail.com', 'foinfo@yahoo.com', 'njfjrnfjrn@yahoo.com', 'nfjebfjen@hotmail.com', 'gnrgiprou@hotmail.com', 'jfei@hotmail.com']})
Run Code Online (Sandbox Code Playgroud)
看起来像:
mail
0 adv@gmail.com
1 fhngn@gmail.com
2 foinfo@yahoo.com
3 njfjrnfjrn@yahoo.com
4 nfjebfjen@hotmail.com
5 gnrgiprou@hotmail.com
6 jfei@hotmail.com
Run Code Online (Sandbox Code Playgroud)
我想要做的是过滤掉(消除)列邮件中的值以“@gmail.com”结尾的所有行。
我有一个pandas数据框,其列为datatime,如下所示:
data.ts_placed
Out[68]:
1 2008-02-22 15:30:40
2 2008-03-20 16:56:00
3 2008-06-14 21:26:02
4 2008-06-16 10:26:02
5 2008-06-23 20:41:03
6 2008-07-17 08:02:00
7 2008-10-13 12:47:05
8 2008-11-14 09:20:33
9 2009-02-23 11:24:18
10 2009-03-02 10:29:19
Run Code Online (Sandbox Code Playgroud)
我想通过在2009年之前消除所有行来对数据帧进行切片
我知道在使用简单函数图时如何绘制两个图:
old.par <- par(mfrow=c(1, 2))
plot(faithful, main="Faithful eruptions")
plot(large.islands, main="Islands", ylab="Area")
par(old.par)
Run Code Online (Sandbox Code Playgroud)
这将返回......
我需要为相当复杂的spplot函数做同样的事情.我想要的是一个3 x 3的正方形.
我要绘制9次的功能是:
labelat = fivenum(gwr.res$SDF$Unempl)
labeltext = labelat
spplot(gwr.res$SDF, "Unempl", cuts = 4, at = c(fivenum(gwr.res$SDF$Unempl)), col.regions = Greens,
colorkey=list(width=0.3,
space="right",
tick.number=5,
labels=list(
at=labelat,
labels=labeltext )))
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题的任何想法?
谢谢,
我有两个列表看起来像:
list1 = ['a','a','b','b','b','c','d','e','e','g','g']
list2 = ['a','c','z','y']
Run Code Online (Sandbox Code Playgroud)
我想要做的是保持list1的所有元素也在list2中.结果应该是:
outcome= ['a','a','c']
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个装饰器以重复N次错误函数,并且之间的睡眠时间不断增加。到目前为止,这是我的尝试:
def exponential_backoff(seconds=10, attempts=10):
def our_decorator(func):
def function_wrapper(*args, **kwargs):
for s in range(0, seconds*attempts, attempts):
sleep(s)
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return function_wrapper
return our_decorator
@exponential_backoff
def test():
for a in range(100):
if a - random.randint(0,1) == 0:
print('success count: {}'.format(a))
pass
else:
print('error count {}'.format(a))
'a' + 1
test()
Run Code Online (Sandbox Code Playgroud)
我不断收到错误:
TypeError: our_decorator() missing 1 required positional argument: 'func'
Run Code Online (Sandbox Code Playgroud) 我有以下简单的功能:
def divide(x, y):
quotient = x/y
remainder = x % y
return quotient, remainder
x = divide(22, 7)
Run Code Online (Sandbox Code Playgroud)
如果我访问变量x我会得到:
x
Out[139]: (3, 1)
Run Code Online (Sandbox Code Playgroud)
有没有办法只得到商或余数?
我有这个pandas数据框:
df = DataFrame({'id':['a','b','b','b','c','c'], 'category':['z','z','x','y','y','y'], 'category2':['1','2','2','2','1','2']})
Run Code Online (Sandbox Code Playgroud)
看起来像:
category category2 id
0 z 1 a
1 z 2 b
2 x 2 b
3 y 2 b
4 y 1 c
5 y 2 c
Run Code Online (Sandbox Code Playgroud)
我想做的是groupby id并返回另外两列作为唯一字符串的串联.
结果如下:
category category2 id
0 z 1 a
1 zxy 2 b
2 y 12 c
Run Code Online (Sandbox Code Playgroud) 我需要创建一个函数来计算数据框列中异常值的百分比.对于异常值,我的意思是任何数据点与均值相差超过3个标准差.
我查看了包装异常值,但这并没有让我感到厌烦,因为所有功能似乎都是针对寻找异常值而不是计算它们.
我可以使用它的功能吗?
我写了一个函数,应该多次尝试一个函数,直到这个工作.
def Retry(attempts,back_off,value):
for i in range(attempts):
counter = 0
while attempts > counter:
try:
x = function(value)
except:
counter =+ 1
delay = (counter * back_off) + 1
print ('trying again in {} seconds'.format(delay))
sleep(delay)
continue
break
return x
result = Retry(20,2,value)
Run Code Online (Sandbox Code Playgroud)
每次失败的尝试之后都应该是指数增长的时间间隔,即2秒后的第二次尝试,4秒后的第三次尝试,8秒后的第四次尝试,依此类推.问题在于,在我写的函数中,如果第一次尝试失败,我只会得到一系列无限的行:
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
....
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?为什么循环堆栈在那里?
我有一个 Pandas df 代表一家商店的营业日,看起来像:
Dates Open
0 2016-01-01 0
1 2016-01-02 0
2 2016-01-03 0
3 2016-01-04 1
4 2016-01-05 1
5 2016-01-06 1
6 2016-01-07 1
7 2016-01-08 1
8 2016-01-09 0
9 2016-01-10 0
10 2016-01-11 1
11 2016-01-12 1
12 2016-01-13 1
13 2016-01-14 1
14 2016-01-15 1
15 2016-01-16 0
16 2016-01-17 0
17 2016-01-18 1
18 2016-01-19 1
19 2016-01-20 1
20 2016-01-21 1
21 2016-01-22 1
22 2016-01-23 0
23 2016-01-24 0
24 2016-01-25 …Run Code Online (Sandbox Code Playgroud)