小编Xip*_*ias的帖子

DataFrame中的字符串,但dtype是对象

为什么Pandas告诉我我有对象,尽管所选列中的每个项都是一个字符串 - 即使在显式转换之后也是如此.

这是我的DataFrame:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 56992 entries, 0 to 56991
Data columns (total 7 columns):
id            56992  non-null values
attr1         56992  non-null values
attr2         56992  non-null values
attr3         56992  non-null values
attr4         56992  non-null values
attr5         56992  non-null values
attr6         56992  non-null values
dtypes: int64(2), object(5)
Run Code Online (Sandbox Code Playgroud)

其中五个是dtype object.我明确地将这些对象转换为字符串:

for c in df.columns:
    if df[c].dtype == object:
        print "convert ", df[c].name, " to string"
        df[c] = df[c].astype(str)
Run Code Online (Sandbox Code Playgroud)

然后,df["attr2"]仍然有dtype object,虽然type(df["attr2"].ix[0]揭示str,这是正确的. …

python types numpy series pandas

86
推荐指数
4
解决办法
4万
查看次数

如何顺序打印jq输出

jq用于处理JSON时,由于长JSON对象,我经常会丢失概述.因此,类似的东西jq . | less会很好.然而,虽然上面的工作,漂亮的着色jq已经消失.

是否有另一种方法可以jq逐行读取输出,或逐窗口读取输出,而不会使用完整的JSON对象发送垃圾邮件?

编辑:这对我不起作用:echo '{"hello": "world"}' | jq . | less -C

io command-line pagination jq

14
推荐指数
2
解决办法
3379
查看次数

递归替换字典中的字符

.给定任意嵌套字典的情况下,如何将所有点更改为下划线(在dict的键中)?

我尝试的是编写两个循环,但后来我将仅限于2级嵌套字典.

这个 ...

{
    "brown.muffins": 5,
    "green.pear": 4,
    "delicious.apples": {
        "green.apples": 2
    {
}
Run Code Online (Sandbox Code Playgroud)

......应该成为:

{
    "brown_muffins": 5,
    "green_pear": 4,
    "delicious_apples": {
        "green_apples": 2
    {
}
Run Code Online (Sandbox Code Playgroud)

有优雅的方式吗?

python string recursion dictionary

13
推荐指数
2
解决办法
1889
查看次数

何时使用matplotlib.pyplot类以及何时使用plot对象(matplotlib.collections.PathCollection)

我想知道问题背后的逻辑是什么时候使用绘图实例(这是一个PathCollection)以及何时使用绘图类本身.

import matplotlib.pyplot as plt
p = plt.scatter([1,2,3],[1,2,3])
Run Code Online (Sandbox Code Playgroud)

提出散点图.为了使它工作,我不得不说:

plt.annotate(...)
Run Code Online (Sandbox Code Playgroud)

并配置轴标签或限制,你写:

plt.xlim(...)
plt.xlabel(...)
Run Code Online (Sandbox Code Playgroud)

等等.

但另一方面,你写道:

p.axes.set_aspect(...)
p.axes.yaxis.set_major_locator(...)
Run Code Online (Sandbox Code Playgroud)

这背后的逻辑是什么?我可以在某处查找吗?不幸的是,我没有在文档中找到这个特定问题的答案.

您何时使用实际实例p来配置图表,何时使用pyplot类plt

python matplotlib

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

使用NumPy数组交换列

当我拥有a=1b=2,我可以a,b=b,a这样写,ab互相交换.

我使用这个矩阵作为数组:

   [ 1,  2,  0, -2]
   [ 0,  0,  1,  2]
   [ 0,  0,  0,  0]
Run Code Online (Sandbox Code Playgroud)

交换numpy数组的列不起作用:

import numpy as np

x = np.array([[ 1,  2,  0, -2],
   [ 0,  0,  1,  2],
   [ 0,  0,  0,  0]])

x[:,1], x[:,2] = x[:,2], x[:,1]
Run Code Online (Sandbox Code Playgroud)

它产生:

   [ 1,  0,  0, -2]
   [ 0,  1,  1,  2]
   [ 0,  0,  0,  0]
Run Code Online (Sandbox Code Playgroud)

所以x[:,1]简单地被覆盖而不是转移到x[:,2].

为什么会这样?

python arrays numpy iterable-unpacking

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

使用值(如果存在),否则使用或运算符指定默认值

我在一本书中找到了这个例子:

// Create _callbacks object, unless it already exists
var calls = this._callbacks || (this._callbacks = {});
Run Code Online (Sandbox Code Playgroud)

我简化了它,以便我不必使用特殊的对象范围:

var a = b || (b = "Hello!");
Run Code Online (Sandbox Code Playgroud)

当定义b时,它可以工作.如果未定义b,则它不起作用并抛出ReferenceError.

ReferenceError: b is not defined
Run Code Online (Sandbox Code Playgroud)

我做错了什么吗?谢谢!

javascript variable-assignment or-operator

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

Mongo退出代码45,无法启动mongod,MongoDB无法打开或获取文件锁定

当我键入mrt(mrt应该启动Meteor应用程序)时,我的Meteor应用程序会不断显示此消息:

Unexpected mongo exit code 45. Restarting.

Unexpected mongo exit code 45. Restarting.

Unexpected mongo exit code 45. Restarting.

Can't start mongod

MongoDB cannot open or obtain a lock on a file
Run Code Online (Sandbox Code Playgroud)

这样做sudo mrt,它的工作原理.

修复其他错误后我得到了这个错误.我输入以下内容修复了之前得到的错误:

sudo chmod -R 777 ./

之前,我有其他错误.但是现在我不知道它可能是什么,因为在该目录中root和我的用户之间不应该存在差异.

对于非Meteor用户:Meteor在其.meteor目录中有一点mongodb .

可能是什么原因?

这是ls -Rl .meteor/ | grep root作为普通用户执行的请求输出:

drwxrwxrwx 4 root root 4096 Jan  3 19:59 local
-rwxrwxrwx 1 root root  220 Jan  3 11:51 packages
-rwxrwxrwx 1 …
Run Code Online (Sandbox Code Playgroud)

mongodb meteor meteorite

7
推荐指数
2
解决办法
4493
查看次数

检测是否闪亮运行R代码

我想在Rnw文件和交互式闪亮的R markdown文档的两个位置中运行R代码。

因此,由于交互式闪亮组件在Rnw文件中不起作用,我需要的是R中的一个代码片段,该片段可检测是否加载交互式代码。

似乎可行,但是感觉像是一个快速hack:

if (exists("input")) { # input is provided by shiny
    # interactive components like renderPlot for shiny
} else {
    # non-interactive code for Rnw file
}
Run Code Online (Sandbox Code Playgroud)

是否有一个稳定的解决方案或我可以访问的全局变量之类的信息,表明当前是否正在运行Shiny?还是应该检查shiny包装是否已装入?

最安全的是什么?

r rstudio shiny rnw

7
推荐指数
2
解决办法
420
查看次数

git中修剪的区别

最初,我想删除一个远程分支.

git push --delete <branchname>
Run Code Online (Sandbox Code Playgroud)

......为我做了.但是,我没有得到其他命令之间的区别:

git push --prune ...
git prune ...
Run Code Online (Sandbox Code Playgroud)

这些是为了什么?手册没有帮助我,因为它听起来很相似,我希望他们做我最初想做的事情.

git

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

在python中反转顺序

假设我有这样的函数:

from toolz.curried import *

@curry
def foo(x, y):
    print(x, y)
Run Code Online (Sandbox Code Playgroud)

然后我可以打电话:

foo(1,2)
foo(1)(2)
Run Code Online (Sandbox Code Playgroud)

两者都返回预期的相同.

但是,我想做这样的事情:

@curry.inverse # hypothetical
def bar(*args, last):
    print(*args, last)

bar(1,2,3)(last)
Run Code Online (Sandbox Code Playgroud)

这背后的想法是我想预先配置一个函数,然后把它放在这样的管道中:

pipe(data,
    f1, # another function
    bar(1,2,3) # unknown number of arguments
)
Run Code Online (Sandbox Code Playgroud)

然后,bar(1,2,3)(data)将被称为管道的一部分.但是,我不知道该怎么做.有任何想法吗?非常感谢你!

编辑:

要求提供一个更具说明性的例子.因此,它来了:

import pandas as pd
from toolz.curried import *

df = pd.DataFrame(data)

def filter_columns(*args, df):
    return df[[*args]]

pipe(df,
    transformation_1,
    transformation_2,
    filter_columns("date", "temperature")
)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,DataFrame通过函数传递,并且filter_columns就是其中之一.但是,该函数已预先配置并返回一个只接受DataFrame的函数,类似于装饰器.这可以实现相同的行为:

def filter_columns(*args):
    def f(df):
        return df[[*args]]
    return f
Run Code Online (Sandbox Code Playgroud)

但是,我总是要运行两个调用,例如filter_columns()(df),这就是我想要避免的.

python currying toolz

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