我有以下简单的说法pandas.DataFrame:
df = pd.DataFrame(
{
"journey": ['ch1', 'ch2', 'ch2', 'ch1'],
"cat": ['a', 'b', 'a', 'c'],
"kpi1": [1,2,3,4],
"kpi2": [4,3,2,1]
}
)
Run Code Online (Sandbox Code Playgroud)
我的情节如下:
import bokeh.plotting as bpl
import bokeh.models as bmo
bpl.output_notebook()
source = bpl.ColumnDataSource.from_df(df)
hover = bmo.HoverTool(
tooltips=[
("index", "@index"),
('journey', '@journey'),
("Cat", '@cat')
]
)
p = bpl.figure(tools=[hover])
p.scatter(
'kpi1',
'kpi2', source=source)
bpl.show(p) # open a browser
Run Code Online (Sandbox Code Playgroud)
我没有按照颜色对点进行颜色编码cat.最终,我希望第一和第三点在相同的颜色,第二和第四点在两种不同的颜色.
如何使用Bokeh实现这一目标?
让我们假设所有数字都是真实的.我试图在mathematica 中获得间隔中的所有值ArcSin.特别是,ArcSinmathematica中 的正常行为ArcSin[x]是在[-Pi/2,Pi/2]间隔中每当x实际存在时[-1,1].
但是,我需要获得上述每个区间的所有角度.有没有办法实现这个目标?[0,2 Pi]x
在elasticsearch中滚动时,重要的是在每个滚动中提供最新的内容scroll_id:
初始搜索请求和每个后续滚动请求将返回一个新的scroll_id?—仅应使用最新的scroll_id。
以下示例(取自此处)使我感到困惑。首先,滚动初始化:
rs = es.search(index=['tweets-2014-04-12','tweets-2014-04-13'],
scroll='10s',
search_type='scan',
size=100,
preference='_primary_first',
body={
"fields" : ["created_at", "entities.urls.expanded_url", "user.id_str"],
"query" : {
"wildcard" : { "entities.urls.expanded_url" : "*.ru" }
}
}
)
sid = rs['_scroll_id']
Run Code Online (Sandbox Code Playgroud)
然后循环:
tweets = [] while (1):
try:
rs = es.scroll(scroll_id=sid, scroll='10s')
tweets += rs['hits']['hits']
except:
break
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是我看不到sid更新的地方。我相信它是在python客户端内部发生的。但我不明白它是如何工作的...
在这个答案中建议的解决方案允许保存dict到json.例如:
import json
with open('data.json', 'wb') as fp:
json.dump(data, fp)
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用3.x.我收到以下错误:
TypeError: 'str' does not support the buffer interface
Run Code Online (Sandbox Code Playgroud)
根据这个答案,解决方案是某种演员; 但我无法为字典做这件事.保存dict到json使用python 3.x 的正确方法是什么?
与此类似的问题.我有非自定义安装,我想知道数据实际存储在哪里.它不是在/var/lib/elasticsearch/nodes/0/indices/{nameOfYourIndex}/(0-4}/index链接问题的已接受答案中指出的.
假设我对每小时的事件数进行计数,如下所示:
np.random.seed(42)
idx = pd.date_range('2017-01-01', '2017-01-14', freq='1H')
df = pd.DataFrame(np.random.choice([1,2,3,4,5,6], size=idx.shape[0]), index=idx, columns=['count'])
df.head()
Out[3]:
count
2017-01-01 00:00:00 4
2017-01-01 01:00:00 5
2017-01-01 02:00:00 3
2017-01-01 03:00:00 5
2017-01-01 04:00:00 5
Run Code Online (Sandbox Code Playgroud)
如果我想知道一周中每天的事件总数,我可以这样做:
df.pivot_table(values='count', index=df.index.dayofweek, aggfunc='sum')
Run Code Online (Sandbox Code Playgroud)
或者
df.groupby(df.index.dayofweek).sum()
Run Code Online (Sandbox Code Playgroud)
两者的产量:
Out[4]:
count
0 161
1 170
2 164
3 133
4 169
5 98
6 172
Run Code Online (Sandbox Code Playgroud)
但是,如果我想计算每个工作日的平均事件数,则如下
df.pivot_table(values='count', index=df.index.dayofweek, aggfunc='mean') # [#1]
Run Code Online (Sandbox Code Playgroud)
是错的!此方法使用总和(如上计算),并将其除以一周中每一天出现的小时数。
我找到的解决方法是:
df_by_day = df.resample('1d').sum()
df_by_day.pivot_table(values='count', index=df_by_day.index.dayofweek, aggfunc='mean')
Run Code Online (Sandbox Code Playgroud)
也就是说,首先重新采样到天数,然后对其进行旋转。不知何故[#1],我觉得这种方法很自然。有没有更pythonic的方式来实现我想要的?为什么不重新采样平均值会被错误地计算?
假设我有以下内容__init__py:
# __init__.py
from . import my_foo
Run Code Online (Sandbox Code Playgroud)
就像这样,flake8会抱怨F401。这可以通过以下方法解决:
# __init__.py
from . import my_foo # NOQA: F401
Run Code Online (Sandbox Code Playgroud)
另一方面,cov插件pytest会抱怨这条线没有测试。这可以通过以下方法解决:
# __init__.py
from . import my_foo # pragma: no cover
Run Code Online (Sandbox Code Playgroud)
我怎样才能让双方都幸福呢?我可以做类似的事情:
# flake8: noqa
from . import gender # pragma: no cover
Run Code Online (Sandbox Code Playgroud)
flake8但从的角度来看,这会影响整个文件。
我也尝试过类似的事情:
from . import gender # pragma: no cover, NOQA: F401
Run Code Online (Sandbox Code Playgroud)
但它并没有按预期工作。
我有以下最少数据:
[
{"date": "2019-01-01", "foo": 10000, "bar": 10, "goo": 30},
{"date": "2019-01-02", "foo": 30000, "bar": 20, "goo": 20},
{"date": "2019-01-03", "foo": 40000, "bar": 20, "goo": 10},
{"date": "2019-01-04", "foo": 1000, "bar": 60, "goo": 20}
]
Run Code Online (Sandbox Code Playgroud)
我使用 VEGA-LITE 绘制的图:
<!DOCTYPE html>
<html>
<head>
<title>Embedding Vega-Lite</title>
<script src="https://cdn.jsdelivr.net/npm/vega@5.4.0"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@3.3.0"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@4.2.0"></script>
</head>
<body>
<div id="vis"></div>
<script type="text/javascript">
var yourVlSpec = {
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"Title": "Insights stats",
"description": "Overview of insights stats",
"width": 1000,
"height": 450,
"data": {
"url": "./data.json"
},
"layer": …Run Code Online (Sandbox Code Playgroud) 我想清除一些列表中的主要事件'a'.也就是说,['a', 'a', 'b', 'b']应该变得['b', 'b']同时['b', 'a', 'a', 'b']保持不变.
def remove_leading_items(l):
if len(l) == 1 or l[0] != 'a':
return l
else:
return remove_leading_items(l[1:])
Run Code Online (Sandbox Code Playgroud)
有更多的pythonic方式吗?
假设我有两个系列:
foo = pd.Series([1,2,3])
bar = pd.Series([7,6,5])
Run Code Online (Sandbox Code Playgroud)
我想从中构建一个数据框:
tmp = pd.DataFrame()
tmp['foo'] = foo
tmp['bar'] = bar
Run Code Online (Sandbox Code Playgroud)
接下来,我设置新数据框的索引:
tmp.index=range(1,4)
Run Code Online (Sandbox Code Playgroud)
最终,tmp这种方式是:
foo bar
1 1 7
2 2 6
3 3 5
Run Code Online (Sandbox Code Playgroud)
但是,以下快捷方式:
pd.DataFrame(
{
"foo": foo,
"bar": bar
},
index=range(1,4)
)
Run Code Online (Sandbox Code Playgroud)
产生以下结果:
bar foo
1 6.0000 2.0000
2 5.0000 3.0000
3 nan nan
Run Code Online (Sandbox Code Playgroud)
索引是正确的但值不是.为什么不一样?如果我设置系列的索引foo和bar创建系列,则第二种方法有效.
考虑一下这句话
msg = 'I got this URL /sf/ask/3334590381/?noredirect=1#comment82268544_47637293 freed'
Run Code Online (Sandbox Code Playgroud)
接下来,我使用现成spaCy的英语处理句子:
import spacy
nlp = spacy.load('en')
doc = nlp(msg)
Run Code Online (Sandbox Code Playgroud)
让我们回顾一下以下输出[(t, t.lemma_, t.pos_, t.tag_, t.dep_) for t in doc]:
[(I, '-PRON-', 'PRON', 'PRP', 'nsubj'),
(got, 'get', 'VERB', 'VBD', 'ROOT'),
(this, 'this', 'DET', 'DT', 'det'),
(URL, 'url', 'NOUN', 'NN', 'compound'),
(/sf/ask/3334590381/?noredirect=1#comment82268544_47637293,
'/sf/ask/3334590381/?noredirect=1#comment82268544_47637293',
'NOUN',
'NN',
'nsubj'),
(freed, 'free', 'VERB', 'VBN', 'ccomp')]
Run Code Online (Sandbox Code Playgroud)
我想改善URL片段的处理。我尤其要:
lemma为stackoverflow.comtag为URL如何使用spaCy?我想用一个正则表达式(如建议在这里)来决定一个字符串是否是URL或不并获得域名。到目前为止,我仍未找到解决方法。
编辑我想我需要一个自定义组件。但是,似乎没有办法将基于正则表达式(或任何其他)的可调用方式放置为patterns。
我想验证一个字符串是一个有效的代码; 特别是,它应该是一对两个字符.想想2个字符的国家代码.使用该re模块我想出了以下内容:
valid = re.compile('([a-zA-Z]){2,2}')
if valid.match(s) and len(s) == 2:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
我觉得这根本不是最佳的.我该如何优化此测试?
我找到了一个有用的宝石(如果你很好奇,这里就是链接).我用它安装了它sudo gem install json_resume.现在它驻留在/Library/Ruby/Gems/2.0.0/gems/json_resume-1.0.4/(Mac OS)上.但是,我需要改变它的一些元素.到目前为止,我/Library直接改变了它们.这显然不是最好的方法.
我想分叉存储库并安装我的版本.我该怎么做?我找到了这个答案,但我对Ruby太新了,不明白该怎么做.
更多细节:安装gem后,我发现了以下脚本/usr/local/bin/json_resume:
#!/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
#!/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'json_resume' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/
version = $1
ARGV.shift
end
end …Run Code Online (Sandbox Code Playgroud)