草庐IT

columns-table

全部标签

python - 如何在 pandas 的 crosstab/pivot_table 中使用两个不同的函数?

使用pandas,是否可以计算包含从两个不同函数计算的值的单个交叉表(或数据透视表)?importpandasaspdimportnumpyasnpc1=np.repeat(['a','b'],[50,50],axis=0)c2=list('xy'*50)c3=np.repeat(['G1','G2'],[50,50],axis=0)np.random.shuffle(c3)c4=np.repeat([1,2],[50,50],axis=0)np.random.shuffle(c4)val=np.random.rand(100)df=pd.DataFrame({'c1':c1,'c2'

python - Pandas read_table 使用第一列作为索引

我这里有个小问题。我有一个txt文件,其中包含以下形式的行(比方说第1行):id1-a1-b1-c1我想使用pandas将其加载到数据框中,索引为id,列名称为“A”、“B”、“C”,值分别为ai、bi、ci最后我希望数据框看起来像:'A''B''C'id1a1b1c1id2a2b2c2............我可能想按block读取文件很大,但假设我一次读取:withopen('file.txt')asf:table=pd.read_table(f,sep='-',index_col=0,header=None,lineterminator='\n')并重命名列table.colum

python - Pandas pivot_table 列名称

对于这样的数据框:d={'id':[1,1,1,2,2],'Month':[1,2,3,1,3],'Value':[12,23,15,45,34],'Cost':[124,214,1234,1324,234]}df=pd.DataFrame(d)CostMonthValueid012411211214223121234315131324145242343342我应用pivot_tabledf2=pd.pivot_table(df,values=['Value','Cost'],index=['id'],columns=['Month'],aggfunc=np.sum,fill_valu

python - django syncdb 通过后 Heroku 出现 "no such table"错误

我正在尝试将我的Django应用程序部署到Heroku。迁移在我本地的Git中。当我尝试时:gitpushherokumasterherokurunpythonmanage.pysyncdb它应用迁移并提示我创建super用户,我成功地做到了。现在应用程序已启动并正在运行,但是当我尝试登录Django管理时,它抛出:OperationalErrornosuchtable:user_user当我尝试herokurunpythonmanage.pymakemigrationsherokurunpythonmanage.pymigrateherokurunpythonmanage.pycre

python - 针对 lexsort : Permutation for sorting each column independently when considering yet another vector 的二维数组广播一维数组

考虑数组anp.random.seed([3,1415])a=np.random.randint(10,size=(5,4))aarray([[0,2,7,3],[8,7,0,6],[8,6,0,2],[0,4,9,7],[3,2,4,3]])我可以创建b,其中包含对每一列进行排序的排列。b=a.argsort(0)barray([[0,0,1,2],[3,4,2,0],[4,3,4,4],[1,2,0,1],[2,1,3,3]])我可以用b对a进行排序a[b,np.arange(a.shape[1])[None,:]]array([[0,2,0,2],[0,2,0,3],[3,4,4

elasticsearch系列-ES对多个字段聚合,select A,B,COUNT() from table group by A,B

**ES对多个字段聚合,selectA,B,**COUNT(*)fromtablegroupbyA,B假设有下表NAMESEXPROF李诚男副教授张旭男讲师王萍女助教刘冰女助教要查询selectSEX,PROF,COUNT(*)fromtablegroupbySEX,PROF1、正确的答案:修改elasticsearch.yml配置文件,添加下面两个配置,重启es集群script.engine.groovy.inline.aggs:onscript.engine.groovy.inline.search:on{"size":0,"query":{"match_all":{}},"aggs":{

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 - PySide-PyQt : How to make set QTableWidget column width as proportion of the available space?

我正在使用PySide开发计算机应用程序,并且我正在使用QTableWidget。假设我的表有3列,但它们包含的数据非常不同,比如(对于每一行)第一列中有一个长句子,然后是最后两列中的3位数字。我希望调整表格大小以根据数据调整其大小,或者至少能够将列大小设置为(比如)70/15/15%的可用空间。执行此操作的最佳方法是什么?在阅读thisquestion后,我尝试了table.horizo​​ntalHeader().setResizeMode(QHeaderView.Stretch)但它使3列大小相同。感谢Fabio,我也尝试了table.horizo​​ntalHeader().s

python 和 Pandas : Combine columns into a date

在我的dataframe中,时间分为3列:year、month、day,例如这个:如何将它们转换成日期,以便进行时间序列分析?我能做到:df.apply(lambdax:'%s%s%s'%(x['year'],x['month'],x['day']),axis=1)给出:10951954111096195412109719541310981954141099195415110019541611011954171102195418110319541911041954110110519541111106195411211071954113但是接下来呢?编辑:这就是我最终得到的:fromda

Python 数据框 : cumulative sum of column until condition is reached and return the index

我是Python的新手,目前面临一个我无法解决的问题。我真的希望你能帮助我。英语不是我的母语,所以如果我不能正确表达自己,我很抱歉。假设我有一个包含两列的简单数据框:indexNum_AlbumsNum_authors01041152443710004144538Num_Abums_tot=sum(Num_Albums)=30我需要对Num_Albums中的数据进行累加,直到达到某个条件。注册满足条件的索引,并从Num_authors中获取对应的值。例子:Num_Albums的累积总和,直到总和等于30的50%±1/15(-->15±2):10=15±2?No,thencontinue