我正在寻找在jupyter笔记本中突出显示SQL代码的方法。我只能突出显示SQL单元魔术,而不能突出显示行魔术和自定义设置。
突出显示单元格魔术(单元格以%% sql开头)
参考:向Jupyter Notebook Cell Magic添加语法突出显示
require(['notebook/js/codecell'], function(codecell) {
codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
Jupyter.notebook.get_cells().map(function(cell){
if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
});
});
Run Code Online (Sandbox Code Playgroud)
换行符:行以%sql开头
我的尝试:将正则表达式更改为,^%sql 但不起作用。
%sql select * from Products limit 5;
Run Code Online (Sandbox Code Playgroud)
如何语法突出显示自定义单元格(单元格以## %%开头)
我的尝试:尝试将正则表达式更改为^##%%sql
##%%sql
q = " select * from customer limit 2;"
execute_query(q,dbname)
Run Code Online (Sandbox Code Playgroud)
在该图中,我们可以看到单元魔术%sql命令未突出显示。我希望它们突出显示。

当我在它们周围徘徊时,我可以看到匹配的括号突出显示为绿色,但是可以向它们添加其他颜色语法,以便匹配的括号具有不同的颜色。
示例代码:
# create new features
df = (df
.withColumn(("rooms_per_hh", F.round(col('total_rooms') / col('households'), 2)))
.withColumn(("pop_per_hh", F.round(col('pop') / col('households'), 2)))
.withColumn(("bedrooms_per_rooms", F.round(col('total_bedrooms') / col('total_rooms'), 2)))
)
Run Code Online (Sandbox Code Playgroud)
我喜欢在这里为不同的括号看到不同的颜色。
相关链接:
- 更改jupyter的匹配括号颜色
- 替换/删除具有自定义主题的Jupyter Notebook中的突出显示
我有一个pandas数据框,其中有很多带有[0][1]字符串的列名,我想知道如何将它们更改为_01等等。
这是我的示例代码:
import numpy as np
import pandas as pd
df = pd.DataFrame({'id':[10,20],'flux[0][0]':[1.1,1.2],
'flux[1][0]':[1.3,1.4],'ellip[2][0]':[1.5,1.6]})
print(df)
flux[0][0] flux[1][0] ellip[2][0] id
0 1.1 1.3 1.5 10
1 1.2 1.4 1.6 20
Run Code Online (Sandbox Code Playgroud)
我的尝试:
df.columns = ['flux_00', 'flux_10', 'ellip_20', 'id']
print(df)
flux_00 flux_10 ellip_20 id
0 1.1 1.3 1.5 10
1 1.2 1.4 1.6 20
Run Code Online (Sandbox Code Playgroud)
但是,对于许多列来说,它花费的时间太长。还有其他更简单的方法吗?
我对熊猫很陌生,所以请耐心和善良。
我正在寻找一种用一条空行替换多个空行的方法,并遇到以下给出的一种解决方案:
:g/^$/,/./-j
Run Code Online (Sandbox Code Playgroud)
我了解以下内容:
g/ replace each occurrences
^$ start to end is an empty, basically empty line
, replace empty line by comma
. maybe repeat last command
-j minus is go up and j is go down
Run Code Online (Sandbox Code Playgroud)
但是,我不明白上面的代码中的句点和减j是如何工作的。Vim是一个非常强大的工具,希望对它的语法有进一步的帮助。
我们在哪里可以找到减j的文档?
周期和减j在这里如何工作?
我想知道如何以列为元素的列来绘制熊猫数据框的多条线图。
例如,我可以使用此数据绘制两个图:
import numpy as np
import pandas as pd
import seaborn as sns
sns.set(color_codes=True)
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.DataFrame({
'pig': [20, 18, 489, 675, 1776],
'horse': [4, 25, 281, 600, 1900]},
index=[1990, 1997, 2003, 2009, 2014])
df.plot.line()
Run Code Online (Sandbox Code Playgroud)
但是,如何为以下数据绘制相似的两个图?
df1 = pd.DataFrame({
'pig': [[20, 18, 489, 675, 1776],[20, 18, 489, 675, 1776]],
'horse': [[14, 25, 271, 700, 1900],[14, 65, 381, 600, 1900]]},
index=['A','B']
)
print(df1)
pig horse
A [20, 18, 489, 675, 1776] [14, 25, 271, …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的pandas系列,如何仅选择索引长度大于3的行?
s = pd.Series([1,2,3,4,5], index=['a','bb','ccc','dddd','eeeee'])
Run Code Online (Sandbox Code Playgroud)
要求的输出:
dddd 4
eeeee 5
Run Code Online (Sandbox Code Playgroud)
我的尝试:
s[len(s.index.name)>3]
Run Code Online (Sandbox Code Playgroud) 我想提取这样的词:
a dog ==> dog
some dogs ==> dog
dogmatic ==> None
Run Code Online (Sandbox Code Playgroud)
有一个类似的链接: 从pandas DataFrame中的文本中提取子字符串作为新列
但这不能满足我的要求。
从此数据帧:
df = pd.DataFrame({'comment': ['A likes cat', 'B likes Cats',
'C likes cats.', 'D likes cat!',
'E is educated',
'F is catholic',
'G likes cat, he has three of them.',
'H likes cat; he has four of them.',
'I adore !!cats!!',
'x is dogmatic',
'x is eating hotdogs.',
'x likes dogs, he has three of them.',
'x likes dogs; he has four of them.', …Run Code Online (Sandbox Code Playgroud)