小编goz*_*lli的帖子

`tapply()`返回数据帧

我有一个数据集,其中包含日期时间(POSIXct),"节点"(因子)和"c"(数字)列,例如:

                 date node           c
1 2011-08-14 10:30:00    2 0.051236000
2 2011-08-14 10:30:00    2 0.081230000
3 2011-08-14 10:31:00    1 0.000000000
4 2011-08-14 10:31:00    4 0.001356337
5 2011-08-14 10:31:00    3 0.001356337
6 2011-08-14 10:32:00    2 0.000000000
Run Code Online (Sandbox Code Playgroud)

对于所有"date"和"node"对,我需要取"c"列的平均值,所以我这样做了:

tapply(data$c, list(data$node, data$date), mean)
Run Code Online (Sandbox Code Playgroud)

我得到的结果是我想要的,但是在一个奇怪的结构中:

num [1:5, 1:8923] 0 0 0.00092 0.00146 NA ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:5] "1" "2" "3" "4" ...
  ..$ : chr [1:8923] "2011-08-14 10:30:00" "2011-08-14 10:31:00" "2011-08-14 10:32:00" "2011-08-14 10:33:00" ...
Run Code Online (Sandbox Code Playgroud)

示例输出将是:

  2011-08-17 23:56:00 2011-08-17 …
Run Code Online (Sandbox Code Playgroud)

r apply

3
推荐指数
1
解决办法
2275
查看次数

Pandas`to_excel`设置字体名称

是否可以使用pandas的excel writer设置字体名称?

这是我的尝试:

import pandas as pd
from pandas.io.excel import ExcelWriter

df = pd.DataFrame([[1,2,3],[4,5,6]])
writer = ExcelWriter("test.xlsx")
writer.set_font_name("Arial")  # this would be ideal, but no such method exists

format = writer.book.add_format({"font_name": "Arial"})
df.to_excel(writer)  # format is not applied. Need to pass `format` object somewhere

writer.close()
Run Code Online (Sandbox Code Playgroud)

xlsxwriter.format.Format一旦创建了对象,我可以在哪里传递它?

python excel pandas xlsxwriter

3
推荐指数
1
解决办法
3153
查看次数

malloc-ating函数中的多维数组

我正在尝试在C程序中分配2d数组.它在这样的主要功能工作正常(如解释在这里):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
    int ** grid;
    int i, nrows=10, ncols=10;
    grid = malloc( sizeof(int *) * nrows);

    if (grid == NULL){
        printf("ERROR: out of memory\n");
        return 1;
    }

    for (i=0;i<nrows;i++){
        grid[i] = malloc( sizeof(int) * ncols);
        if (grid[i] == NULL){
            printf("ERROR: out of memory\n");
            return 1;
        }
    }
    printf("Allocated!\n");

    grid[5][6] = 15;
    printf("%d\n", grid[5][6]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但由于我必须使用不同的数组多次这样做,我试图将代码移动到一个单独的函数中.

#include <stdio.h>
#include <stdlib.h>

int malloc2d(int ** grid, int nrows, int …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers multidimensional-array

2
推荐指数
1
解决办法
7765
查看次数

在朱莉娅变量中的数组结束

我想a在一个循环中切片Julia中的一个数组,以便将它分成几个n样本块.该阵列的长度nsamples的倍数n,所以最后步幅将更短.

我的尝试是使用三元运算符来检查步幅的大小是否大于数组的长度:

for i in 0:n:nsamples-1
    end_ = i+n < nsamples ? i+n : end
    window = a[i+1:end_]
end
Run Code Online (Sandbox Code Playgroud)

这样,如果我超过了数组的大小,a[i+1:end_]就会解决a[i+1:end].

但是,在第2行中使用关键字"end"是不可接受的(它也是julia中"end of control statement"的关键字.

在Python中,我可以分配Noneend_,这将解决到a[i+1:None],这将是数组的结尾.

我怎么能绕过这个?

arrays julia

2
推荐指数
1
解决办法
3679
查看次数

过滤标签列表

我正在尝试选择我的Django数据库中的所有歌曲,其标签是给定列表中的任何一个.有一个Song模型,一个Tag模型和一个SongTag模型(用于多对多关系).

这是我的尝试:

taglist = ["cool", "great"]
tags = Tag.objects.filter(name__in=taglist).values_list('id', flat=True)
song_tags = SongTag.objects.filter(tag__in=list(tags))
Run Code Online (Sandbox Code Playgroud)

此时我收到一个错误:

DatabaseError: MultiQuery does not support keys_only.
Run Code Online (Sandbox Code Playgroud)

我错了什么?如果您可以建议一个完全不同的方法来解决问题,那么它也会受到欢迎!

编辑:我应该提到我正在使用django-nonrel 在Google AppEngine上使用Django

django google-app-engine filter multi-query

1
推荐指数
1
解决办法
516
查看次数

android - 将ToggleButton转换为子类

我可能会误解类型转换,但这是我的问题.

我有一个带有ToggleButton的Android视图:

<LinearLayout
    ... >
    <ToggleButton
        android:id="@+id/btnRec"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:textOff="Start TEST"
        android:textOn="Stop TEST" />
    ...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后在我的活动中我声明了一个ToggleButton变量:

private ToggleButton mRecordButton = null;
Run Code Online (Sandbox Code Playgroud)

现在,我实现了一个名为RecordButton的ToggleButton子类:

class RecordButton extends ToggleButton {

    OnCheckedChangeListener clicker = new OnCheckedChangeListener() {
        ...
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setOnCheckedChangeListener(clicker);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后在onCreate我的ID中找到按钮:

mRecordButton = (RecordButton) findViewById(R.id.btnRec);
Run Code Online (Sandbox Code Playgroud)

这引发了一个ClassCastException.

为什么?如何RecordButtonToggleButton(超类)将其转换为(子类)?

java android casting

1
推荐指数
1
解决办法
310
查看次数

Django 站点地图 - 在 URL 前加双 http://

在 Django 1.4.12 中,我有一个 Sitemap 类:

class MySitemap(Sitemap):
    def items(self):
        return ['/my/url1/',
                '/my/url2/',]

    def location(self, obj):
        return str(obj)
Run Code Online (Sandbox Code Playgroud)

并在 urls.py

sitemaps = {
            'global': MySitemap,
            }

...

urlpatterns = patterns('',
...
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
Run Code Online (Sandbox Code Playgroud)

但是,我生成的sitemap.xmlhttp://http://前面已经有了,比如:

...
<url><loc>http://http://mywebsite.com/my/url1</loc></url>
...
Run Code Online (Sandbox Code Playgroud)

是什么导致了这个问题?

python sitemap django

1
推荐指数
1
解决办法
550
查看次数