为什么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,这是正确的. …
当jq用于处理JSON时,由于长JSON对象,我经常会丢失概述.因此,类似的东西jq . | less会很好.然而,虽然上面的工作,漂亮的着色jq已经消失.
是否有另一种方法可以jq逐行读取输出,或逐窗口读取输出,而不会使用完整的JSON对象发送垃圾邮件?
编辑:这对我不起作用:echo '{"hello": "world"}' | jq . | less -C
.在给定任意嵌套字典的情况下,如何将所有点更改为下划线(在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)
有优雅的方式吗?
我想知道问题背后的逻辑是什么时候使用绘图实例(这是一个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?
当我拥有a=1和b=2,我可以a,b=b,a这样写,a并b互相交换.
我使用这个矩阵作为数组:
[ 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].
为什么会这样?
我在一本书中找到了这个例子:
// 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)
我做错了什么吗?谢谢!
当我键入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) 我想在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包装是否已装入?
最安全的是什么?
最初,我想删除一个远程分支.
git push --delete <branchname>
Run Code Online (Sandbox Code Playgroud)
......为我做了.但是,我没有得到其他命令之间的区别:
git push --prune ...
git prune ...
Run Code Online (Sandbox Code Playgroud)
这些是为了什么?手册没有帮助我,因为它听起来很相似,我希望他们做我最初想做的事情.
假设我有这样的函数:
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 ×5
numpy ×2
arrays ×1
command-line ×1
currying ×1
dictionary ×1
git ×1
io ×1
javascript ×1
jq ×1
matplotlib ×1
meteor ×1
meteorite ×1
mongodb ×1
or-operator ×1
pagination ×1
pandas ×1
r ×1
recursion ×1
rnw ×1
rstudio ×1
series ×1
shiny ×1
string ×1
toolz ×1
types ×1