- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
给定以下 DataFrame,分组为:
dataset = z.groupby(
['app', 'regmonth', 'loginsmonth']).sum().unstack().fillna(
0, inplace=False)
cnt
loginsmonth 2014-02-01 2014-03-01 2014-04-01 2014-05-01
app regmonth
1 2014-02-01 6069 1837 107 54
2014-03-01 0 10742 2709 1394
2014-04-01 0 0 5584 1107
2014-05-01 0 0 0 3044
2014-06-01 0 0 0 0
我想将其转换为:
cnt
loginsmonth 2014-02-01 2014-03-01 2014-04-01 2014-05-01
app regmonth
1 2014-02-01 6069 1837 107 54
2014-03-01 10742 2709 1394 0
2014-04-01 5584 1107 0 0
2014-05-01 3044 0 0 0
2014-06-01 0 0 0 0
因此,它将对角线移动到行的开头并用零填充空白。panda有什么简单的方法吗?
最佳答案
但是您在此过程中正在更改数据,对吗?
我不知道 pandas
是否有好的方法,但是 np.diagnoal
可以在这里做你想做的事:
In [96]:
print df
loginsmonth 2014-02-01 2014-03-01 2014-04-01 2014-05-01
app regmonth
1 2014-02-01 6069 1837 107 54
2014-03-01 0 10742 2709 1394
2014-04-01 0 0 5584 1107
2014-05-01 0 0 0 3044
2014-06-01 0 0 0 0
[5 rows x 4 columns]
In [124]:
print df*0+np.asarray([np.hstack((np.diagonal(df.values, i), np.zeros(i+1, int)))
for i in range(df.shape[1])]).T
loginsmonth 2014-02-01 2014-03-01 2014-04-01 2014-05-01
app regmonth
1 2014-02-01 6069 1837 107 54
2014-03-01 10742 2709 1394 0
2014-04-01 5584 1107 0 0
2014-05-01 3044 0 0 0
2014-06-01 0 0 0 0
[5 rows x 4 columns]
这里 np.zeros(i+1, int)
中的 1
是 df.shape[0]-df.shape[1]
。我不知道你是否会遇到 df.shape[0]
<df.shape[1]
的情况。
但是如果您的 DataFrame
看起来总是像这里显示的那样,它就像一个内部没有 0
的上对角矩阵,您可以走捷径:
In [134]:
print df.apply(lambda x: sorted(x, key=lambda y: y==0), axis=1)
cnt 2014-02-01 2014-03-01 2014-04-01 2014-05-01
app regmonth
1 2014-02-01 6069 1837 107 54
2014-03-01 10742 2709 1394 0
2014-04-01 5584 1107 0 0
2014-05-01 3044 0 0 0
2014-06-01 0 0 0 0
[5 rows x 4 columns]
关于 python Pandas : transforming - moving values from diagonal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24044492/
C++ 中的标准“映射”容器允许您插入右值: T x; std::map m; // m[1]; // populate "1" auto it = m.insert(std::make_pair(
我们知道 std::move does not actually move anything .它只是将左值引用 (&) 转换为右值引用 (&&)。 那么在下面的例子中,拷贝构造函数是如何被调用的呢?
http://en.cppreference.com/w/cpp/language/rule_of_three 几个月前我开始使用 c++11并观看了五人规则。 所以..我开始将复制构造函数/复制赋值
这个问题在这里已经有了答案: In what scenarios should I expect to explicitly need to implement a move constructor
我有一个类似于下面的对象,我正在尝试为它实现一个 move 构造函数,这样你就可以为 std::vector 插入一个. struct Mesh { std::vector vPoint
这个问题在这里已经有了答案: How to create an std::function from a move-capturing lambda expression? (3 个回答) 关闭7年前
我有一个源文件,我正在使用它 move 到一个存档目录 关闭。 move (srcfile,dstdir) 但是当存档目标目录中已经存在相同的文件时,它会抛出一个错误,指出无法 move 文件已经存在
这应该有效,但无效并给出以下错误(如下)。 我读过几篇关于 stackoverflow 的帖子 here和 here但在这种情况下似乎没有一个好的答案。我真的希望我只是错过了一些愚蠢的东西,我已经在这
我似乎无法弄清楚为什么会这样。当我运行以下代码时: $uref = APACHE_ROOT . UPLOAD_PATH . $applicant . "_ref_{$email}_{$year}";
我似乎无法弄清楚为什么会这样。当我运行以下代码时: $uref = APACHE_ROOT . UPLOAD_PATH . $applicant . "_ref_{$email}_{$year}";
我的表格行可以上下 move ,但我的问题是数据表行取代了表格标题(第一行)。 我想要一个固定的第一行,这样当您单击向上箭头时,您就不会向上 move 该行来替换标题。 我尝试了一些条件逻辑来检查当前
正如我在Move constructor/operator=中询问的那样,过了一段时间,我同意并接受了这个问题的正确答案,我只是在想,是否有类似“移动析构函数” 这样的东西会在每次移动的对象上调用会有
如果我有一个像这样的 C 类: class C { std::string s; public: C(std::string& s) : s(s) {} C(std::str
我是 C++11 的新手,发现 move 语义和复制省略非常适合编写优雅高效的代码。不过我有一些问题想请教。这里我写了一个模板类 matrix.hpp 并用它来测试 move 语义的行为。 #incl
我在我们的项目中遇到了这样的代码: class A { public: A(A&& obj): valid_(false), data_(obj.data_) {} //... void
move 语义在这个例子中是如何工作的: struct test { int ii[10]; int i; }; test f() { test a; std::cou
假设我有一个类型为 A 的对象 a。 如果我想将其 move 到函数foo(A)。 一个选择是执行 foo(std::move(a)),这将调用 move 构造函数。 但是,假设我正在使用一个我无法控
我用 move 复制构造函数和 move 复制赋值运算符创建了一个简单的应用程序,并且在它们中的每一个上我都做了一个 cout 语句来告诉我,它们正在执行。但是在执行过程中,我没有看到 move 复制
相关问题: Why this move constructor is not called wtih rvalue temporary? [duplicate] Move Constructor vs
我正在努力研究 move 构造函数,并希望通过这个问题获得更多见解。这是一个简单的类。 class A { private: vector Bs; public: /* ..
我是一名优秀的程序员,十分优秀!