草庐IT

application_apply

全部标签

python - Pandas 数据框 : how to apply describe() to each group and add to new columns?

df:namescoreA1A2A3A4A5B2B4B6B8想要以下面的形式获取以下新数据框:namecountmeanstdmin25%50%75%maxA53............B45............如何从df.describe()中提取信息并重新格式化?谢谢 最佳答案 还有更短的:)printdf.groupby('name').describe().unstack(1)Nothingbeatsone-liner:In[145]:printdf.groupby('name').describe().reset_in

python - 回调 celery apply_async

我在我的应用程序中使用celery来运行周期性任务。让我们看下面的简单示例frommyqueueimportQueue@perodic_task(run_every=timedelta(minutes=1))defprocess_queue():queue=Queue()uid,questions=queue.pop()ifuidisNone:returnjob=group(do_stuff(q)forqinquestions)job.apply_async()defdo_stuff(question):try:...except:...raise正如您在上面的示例中看到的,我使用ce

python - Flask ('application' ) 与 Flask(__name__)

在官方Quickstart,建议在使用单个模块时使用__name__:...Ifyouareusingasinglemodule(asinthisexample),youshoulduse__name__becausedependingonifit’sstartedasapplicationorimportedasmodulethenamewillbedifferent('__main__'versustheactualimportname)....然而,在他们的APIdocument,当我的应用程序是一个包时,建议硬编码:Soit’simportantwhatyouprovideth

python - 在 DataFrame 对象上使用 rolling_apply

我正在尝试滚动计算成交量加权平均价格。为此,我有一个函数vwap可以为我执行此操作,如下所示:defvwap(bars):return((bars.Close*bars.Volume).sum()/bars.Volume.sum()).round(2)当我尝试将此函数与rolling_apply一起使用时,如图所示,出现错误:importpandas.io.dataaswebbars=web.DataReader('AAPL','yahoo')printpandas.rolling_apply(bars,30,vwap)AttributeError:'numpy.ndarray'obj

python - map_async 与 apply_async :what should I use in this case

我正在处理一些ascii数据,进行一些操作,然后将所有内容写回另一个文件(由post_processing_0.main完成的工作,不返回任何内容)。我想将代码与多处理模块并行化,请参见以下代码片段:frommultiprocessingimportPoolimportpost_processing_0defchunks(lst,n):return[lst[i::n]foriinxrange(n)]defmain():pool=Pool(processes=proc_num)P={}foriinrange(0,proc_num):P['process_'+str(i)]=pool.ap

android - 运行时异常 : Unable to instantiate application

当我运行我的应用程序时,每次我的logcat中都会出现以下异常:04-1409:29:53.965:W/dalvikvm(1020):threadid=1:threadexitingwithuncaughtexception(group=0x409c01f8)04-1409:29:53.985:E/AndroidRuntime(1020):FATALEXCEPTION:main04-1409:29:53.985:E/AndroidRuntime(1020):java.lang.RuntimeException:Unabletoinstantiateapplicationandroid.

android - 运行时异常 : Unable to instantiate application

当我运行我的应用程序时,每次我的logcat中都会出现以下异常:04-1409:29:53.965:W/dalvikvm(1020):threadid=1:threadexitingwithuncaughtexception(group=0x409c01f8)04-1409:29:53.985:E/AndroidRuntime(1020):FATALEXCEPTION:main04-1409:29:53.985:E/AndroidRuntime(1020):java.lang.RuntimeException:Unabletoinstantiateapplicationandroid.

python - 使用 apply 向现有数据框添加 2 个新列

我想使用apply函数:-将2列作为输入-基于函数输出两个新列。一个例子是这个add_multiply函数。#functionwith2columninputsand2outputsdefadd_multiply(a,b):return(a+b,a*b)#exampledataframedf=pd.DataFrame({'col1':[1,2],'col2':[3,4]})#thisdoesn'tworkdf[['add','multiply']]=df.apply(lambdax:add_multiply(x['col1'],x['col2']),axis=1)理想的结果:col1c

python - Pandas .apply() 函数中的异常处理

如果我有一个DataFrame:myDF=DataFrame(data=[[11,11],[22,'2A'],[33,33]],columns=['A','B'])提供以下数据框(从stackoverflow开始,没有足够的声誉来获取数据框的图像)|A|B|0|11|11|1|22|2A|2|33|33|如果我想将B列转换为int值并删除无法转换的值,我必须这样做:defconvertToInt(cell):try:returnint(cell)except:returnNonemyDF['B']=myDF['B'].apply(convertToInt)如果我只做:myDF['B']

python - 无法使用 urllib2 将内容类型设置为 application/json

这个小宝贝:importurllib2importsimplejsonasjsonopener=urllib2.build_opener()opener.addheaders.append(('Content-Type','application/json'))response=opener.open('http://localhost:8000',json.dumps({'a':'b'}))产生以下请求(如使用ngrep所见):sudongrep-q-dlo'^POST.*localhost:8000'T127.0.0.1:51668->127.0.0.1:8000[AP]POST/