根据这个帖子: SO:要列出的列名
将列名转换为列表应该很简单.但如果我这样做:
df.columns.tolist()
Run Code Online (Sandbox Code Playgroud)
我明白了:
[u'q_igg', u'q_hcp', u'c_igg', u'c_hcp']
Run Code Online (Sandbox Code Playgroud)
我知道,我可以摆脱你和'.但我想在没有任何黑客的情况下获得干净的名字作为列表.那可能吗 ?
我确实使用散景来绘制本地 LAN 上的传感器数据。Bokeh 是从我的 python 应用程序中使用 popen 启动的:Popen("bokeh serve --host=localhost:5006 --host=192.168.8.100:5006", shell=True)
我想从应用程序中关闭散景服务器。但是,我在文档中找不到任何内容。也bokeh serve --help没有给出任何提示如何做到这一点。
编辑:基于接受的答案,我想出了以下解决方案:
self.bokeh_serve = subprocess.Popen(shlex.split(command),
shell=False, stdout=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
我用来self.bokeh_serve.kill()结束进程。也许.terminate()会更好。我会尝试。
这个问题与SO中的这个问题有关(matplotlib-change-colormap-tab20-to-have-three-colors)
我想以一种方式调整tab10色彩图,我可以按照我想要的步骤更改每种颜色的alpha级别.下面是一个示例(对于具有3个alpha级别的9种颜色),它不会产生预期的输出.此外,它不够通用(因为if elif staements).
任何想法我怎么能这样做?
在这个例子中,我有3个组,有3个子组:
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
n_feature = 3
sub_feature = 3
col = []
for index in range(n_feature*sub_feature):
# loop over colors and change the last entry in descending order 3 times
col.append(list(plt.cm.tab10(index)))
i = 0
for item in col:
# loop over colors and change the last entry in descending order 3 times
if i == 0:
item[-1] = 0.9
i+=1
elif …Run Code Online (Sandbox Code Playgroud) 如果您想过滤列值中包含字符串的行,可以使用类似的内容data.sample_id.str.contains('hph')(之前回答过:检查 pandas dataframe 列中的字符串是否在 list 中,或Check if string is in a pandas dataframe) 。
但是,我的查找列包含空单元格。因此,str.contains()产生NaN值,并且在索引时出现值错误。
`ValueError: cannot index with vector containing NA / NaN values``
Run Code Online (Sandbox Code Playgroud)
什么有效:
# get all runs
mask = [index for index, item in enumerate(data.sample_id.values) if 'zent' in str(item)]
Run Code Online (Sandbox Code Playgroud)
str.contains()有没有比这个更优雅、更快的方法(类似)?
我使用以下链接使用 openblas 编译 Ipopt:./configure --with-blas-incdir="-L/home/moritz/build/CoinIpopt_test/ThirdParty/openblas/include/" --with-blas-lib="-L/home/moritz/build/CoinIpopt_test/ThirdParty/openblas/lib/ -libopenblas_sandybridgep-r0.2.14.a"
如果我没有定义任何特定的 LAPACK 库,Ipopt 会自动使用 netlib 中的 LAPACK。openplas 是否有自己优化的 LAPACK 实现?
如果我使用--with-lapack-incdir="-L/home/moritz/build/CoinIpopt_test/ThirdParty/openblas/include/" --with-lapack-lib="-L/home/moritz/build/CoinIpopt_test/ThirdParty/openblas/lib/"
make 失败,因为无法解析某些 Lapack 例程,例如undefined reference todpotrs_
At least there is the filelapacke.h in theinclude folder but there are not LAPACK libraries in thelib` 文件夹。
我在 GitHub 上发现了这个问题:
好的,我想我想通了...您可以通过在 make 命令行上指定 NO_LAPACK=1 来构建 OpenBLAS,而不包含 LAPACK。默认情况下,它包含完整的 LAPACK 库(某些部分已优化)。
但如果是这种情况,库在哪里?
我应该针对 openblas 编译 LAPACK 吗?
如果有人能够阐明这个问题,我将不胜感激。
来自文档:
# Declare both screens
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
Run Code Online (Sandbox Code Playgroud)
从这个SO问题:
class WelcomeScreen(Screen):
def __init__(self, **kwargs):
super(Screen,self).__init__(**kwargs)
Run Code Online (Sandbox Code Playgroud)
在哪种情况下需要初始化屏幕,super为什么?
我通过调整图的颜色顺序 rcParams['axes.color_cycle'] = [some nice and carefully chosen colours]
但是,当我将twinx用于第二个轴时,将重置颜色顺序:
from matplotlib import pyplot as plt
from matplotlib import rcParams
rcParams['axes.color_cycle'] = ['r','g','k']
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax1 = plt.twinx(ax)
ax.plot([1,2,3],[4,5,6])
ax.plot([1,2,3],[7,6,4])
ax1.plot([1,2,3],[5,3,1])
Run Code Online (Sandbox Code Playgroud)
有办法避免这种情况吗?画在上面的线ax1应该是黑色的。
以下代码生成一个条形图,其中xticklabels以每个条形为中心.但是,缩放x轴,更改条数或更改条宽会改变标签的位置.是否存在处理该行为的通用方法?
# This code is a hackish way of setting the proper position by trial
# and error.
import matplotlib.pyplot as plt
import numpy as np
y = [1,2,3,4,5]
# adding 0.75 did the trick but only if I add a blank position to `xl`
x = np.arange(0,len(y)) + 0.75
xl = ['', 'apple', 'orange', 'pear', 'mango', 'peach']
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x,y,0.5)
ax.set_xticklabels(xl)
# I cannot change the scaling without changing the position of the tick labels …Run Code Online (Sandbox Code Playgroud) 按模式拆分列很容易:
import pandas as pd
_df = pd.DataFrame([['1 / 2 / 3', '4 / 5 / 6'], ['7 / 8 / 9', '10 / 11 / 12']])
_df.apply(lambda x: x.str.split(' / '))
0 1
0 [1, 2, 3] [4, 5, 6]
1 [7, 8, 9] [10, 11, 12]
Run Code Online (Sandbox Code Playgroud)
但是如何使用多expand=True索引创建数据帧?我不知道我可以在哪里传递索引。
_df.apply(lambda x: x.str.split(' / ', expand=True))
ValueError: If using all scalar values, you must pass an index
Run Code Online (Sandbox Code Playgroud)
预期输出(列名不重要,可以是任意的):
A B
a b c a b c
0 1 2 …Run Code Online (Sandbox Code Playgroud) 我对此感到非常困惑。也许有人可以为我澄清结果:
import numpy as np
a = np.array([ 1.97635114, 1.72790352, 1.51046621, 1.25543557, 1.01718594,
0.77378732, 0.53452001, 0.29627038, 0.05802074, -0.18022889,
-0.41847852])
e = -1.377404416
a**e
Run Code Online (Sandbox Code Playgroud)
产生
array([ 0.39126903, 0.47080342, 0.56661974, 0.73100461, 0.97680242, 1.42368231, 2.369756 , 5.34193942, 50.47146058, nan, nan])
[item**e for item in a]
Run Code Online (Sandbox Code Playgroud)
给我
[0.39126902696026267, 0.4708034172873469, 0.5666197367017416, 0.7310046117775049, 0.9768024208132248,
1.4236823077174863, 2.369756002432608, 5.341939422216064,
50.47146057971127, nan, nan]
但
-0.41847852**e
Run Code Online (Sandbox Code Playgroud)
产量
-3.3197780173988067
Run Code Online (Sandbox Code Playgroud)
为什么numpy无法计算这两个数字的指数?
散景0.10的文档是可以的,并且有许多好例子.但是,我现在不知道如何告诉散景0.11使用容易记住的网址.我目前的尝试:
导入numpy作为np导入时间
from bokeh.client import push_session
from bokeh.plotting import figure, curdoc, output_server
x = np.random.rand(10)
y = np.random.rand(10)
p = figure()
r2 = p.line(x, y, color="navy", line_width=4)
# open a session to keep our local document in sync with server
session = push_session(curdoc())
session.show() # open the document in a browser
time.sleep(2)
while True:
x = np.random.rand(10)
y = np.random.rand(10)
r2.data_source.data["y"] = y
r2.data_source.data["x"] = x
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
来自Docs:
push_session(curdoc(),session_id = 'yeah')
然而,网址仍然有点笨拙:http://localhost:5006/?bokeh-session-id=yeah
有没有办法改变它http://localhost:5006/yeah? …
以下函数是优化问题的一部分。它非常简单,但经常被调用。如果速度能提高一点就好了。我尝试过 Numba,但似乎我必须用 FORTRAN 编写它:
\n\nimport numpy as np\nfrom numba import autojit\n# I am using numbapro: from numbapro import autojit\n\n# minimal dataset\nn_comp_glob = 2\nx_glob = np.random.rand(3*n_comp) \nqs_glob = np.array([100.])\ncp_glob =np.tile(1.,n_comp)\ncs_glob = np.array( [100.])\n\ndef get_denom(n_comp,qs,x,cp,cs_f):\n k = x[0:n_comp]\n sigma = x[n_comp:2*n_comp]\n z = x[2*n_comp:3*n_comp]\n # calculates the denominator in Equ 14a - 14c (Brooks & Cramer 1992)\n a = 0.0 \n\n for i in range(n_comp):\n a += (sigma[i] + z[i])*( k[i]*(qs/cs)**(z[i]-1) )*cp[i]\n\n return denom\n\nget_denom_jit=autojit(get_denom)\n\nimport timeit\n%timeit get_denom(n_comp_glob,qs_glob,x_glob,cp,cs_glob)\n10000 loops, best of 3: 22.9 …Run Code Online (Sandbox Code Playgroud) 考虑以下数据框:
{('CA', 'ca'): {('A', 'a'): 2,
('A', 'aa'): 2,
('B', 'b'): 2,
('B', 'bb'): 2,
('C', 'c'): 2,
('C', 'cc'): 2},
('CA', 'cb'): {('A', 'a'): 1,
('A', 'aa'): 1,
('B', 'b'): 1,
('B', 'bb'): 1,
('C', 'c'): 1,
('C', 'cc'): 1}}
Run Code Online (Sandbox Code Playgroud)
如何将数据框转换为可以通过与此类似的复制和粘贴插入到文档字符串中的布局?
+-------------+----------+---------+
| First | Second | Third |
+=============+==========+=========+
| ('A', 'a') | 2 | 1 |
+-------------+----------+---------+
...
Run Code Online (Sandbox Code Playgroud)