我有一堆字母,不能为我的生活弄清楚如何将它们转换为相当于它们的数字.
letters[1:4]
Run Code Online (Sandbox Code Playgroud)
有功能吗?
numbers['e']
Run Code Online (Sandbox Code Playgroud)
返回
5
Run Code Online (Sandbox Code Playgroud)
或用户定义的东西(即1994年)?
我想将所有26个字母转换为特定值.
我正在分析推文。
我有 10k 条推文,并且对出现的单词列表感兴趣:
lst1=['spot','mistake']
lst1_tweets=tweets[tweets['tweet_text'].str.contains('|'.join(lst1))].reset_index()
Run Code Online (Sandbox Code Playgroud)
我想仔细检查一下:
f=lst1_tweets['tweet_text'][0]
f='Spot the spelling mistake Welsh and Walsh. You are showing picture of presenter Bradley Walsh who is alive and kick'
type(f)
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
我用了
f.str.contains('|'.join(lst1))
Run Code Online (Sandbox Code Playgroud)
返回:
AttributeError: 'str' object has no attribute 'str'
Run Code Online (Sandbox Code Playgroud)
还
f.contains('|'.join(lst1))
Run Code Online (Sandbox Code Playgroud)
返回:
AttributeError: 'str' object has no attribute 'contains'
Run Code Online (Sandbox Code Playgroud)
任何关于如何在字符串中搜索单词列表的建议
我想在R中打印屏幕双引号("),但它不起作用.典型的正则表达式转义符不起作用:
> print('"')
[1] "\""
> print('\"')
[1] "\""
> print('/"')
[1] "/\""
> print('`"')
[1] "`\""
> print('"xml"')
[1] "\"xml\""
> print('\"xml\"')
[1] "\"xml\""
> print('\\"xml\\"')
[1] "\\\"xml\\\""
Run Code Online (Sandbox Code Playgroud)
我希望它返回:
" "xml" "
Run Code Online (Sandbox Code Playgroud)
我将在下游使用它.
有任何想法吗?
我是闪亮的新手,正在制作一个应用程序,用户可以在其中调整 3 个滑块。输出是 3 个滑块的总变化(以 % 形式表示)以及与初始形式的差异。
我添加了一个“重置”按钮,以便我可以将滑块全部更改回 0,但它不起作用。
我尝试寻找Rshiny:resetplottodefaultstate和Resetinputswithreactiveappinshiny,但无济于事。我尝试了一段代码:
observeEvent(input$reset,{
input$Var1=0
return(input$Var1)
})
Run Code Online (Sandbox Code Playgroud)
调整单个滑块并将其设置为0,然后返回该值,同样无济于事。有什么建议么?
我的完整代码是:
library(shiny)
ui=fluidPage(
titlePanel("Sum of Sliders"),
fluidRow(
column(2,
sliderInput("Var1", "Slider1:",
min = -100, max = 100,
value = 0),
sliderInput("Var2", "Slider2:",
min = -100, max = 100,
value = 0),
sliderInput("Var3", "Slider3:",
min = -100, max = 100,
value = 0),
submitButton("Submit"),
actionButton("reset", "Reset")
),
column(6,
verbatimTextOutput("text"),
verbatimTextOutput("text2"),
verbatimTextOutput("change")
)
)
)
server=function(input, output) {
observeEvent(input$reset,{
input$Var1=0
return(input$Var1)
})
output$text <- …
Run Code Online (Sandbox Code Playgroud) I have some text, and I want to highlight specific words. I wrote a script to loop through the words and highlight the desired text, but how do I set this up to return it to sentences?
from termcolor import colored
text = 'left foot right foot left foot right. Feet in the day, feet at night.'
l1 = ['foot', 'feet']
for t in text.lower().split():
if t in l1:
print(colored(t, 'white', 'on_red'))
else: print(t)
Run Code Online (Sandbox Code Playgroud)
In the above example, I want …
我正在做 PCA,有 10 个组件。我想根据它们的组类型绘制前 3 个组件和颜色。
from mpl_toolkits.mplot3d import Axes3D
df=pd.DataFrame(np.random.rand(30,20))
grp=round(pd.DataFrame(np.random.rand(30)*10),0)
df['grp']=grp
fig = plt.figure(figsize=(12, 9))
ax = Axes3D(fig)
y = df.iloc[:,1]
x = df.iloc[:,0]
z = df.iloc[:,2]
c = df['grp']
ax.scatter(x,y,z, c=c, cmap='coolwarm')
plt.title('First 3 Principal Components')
ax.set_ylabel('PC2')
ax.set_xlabel('PC1')
ax.set_zlabel('PC3')
plt.legend()
Run Code Online (Sandbox Code Playgroud)
这是可行的,但不幸的是并没有显示一个传说,也不相信所有可能的群体。
我有一个数据框和一个名称向量:
df=data.frame(col1=letters[1:3],col2=rnorm(3))
v=c(a=2,b=4,c=56,d=65)
Run Code Online (Sandbox Code Playgroud)
我想合并它们,并只保留数据框中的值
v=data.frame(v)
merge(df,v,by.x='col1',by.y=row.names,all.x=TRUE)
Error in as.vector(x, mode) :
cannot coerce type 'closure' to vector of type 'any'
Run Code Online (Sandbox Code Playgroud)
我想要:
col1 rnorm.3. v
1 a 0.6182781 2
2 b 0.9559001 4
3 c -0.5459661 56
Run Code Online (Sandbox Code Playgroud)
注意我的真实数据是1M行和1.5M命名向量
类似于在 pandas 中按索引选择多个行,我有大数据框,并且有一些我有兴趣查看的索引。
我有:
import pandas as pd
df = pd.DataFrame({'A':[0,1,2,3,4,5,6,7,8,9],'B':['a','b','c','d','e','f','g','h','i','j']},
index=range(10,20,))
Run Code Online (Sandbox Code Playgroud)
我想看到的索引很少, list=[12,15,10,14]
所以我最终得到:
A B
12 2 c
15 5 f
10 0 a
14 4 e
Run Code Online (Sandbox Code Playgroud)
有没有命令让我可以去:
df.iloc[[index isin list]]
Run Code Online (Sandbox Code Playgroud)
因为值列表是由较早的一段代码制成的。
我试过:
df.loc[[ix]]
Run Code Online (Sandbox Code Playgroud)
哪里ix=dm.iloc[250].sort_values()[:10].index
和是Int64Index([250, 1109, 427, 513, 678, 18, 697, 1075, 533, 739], dtype='int64')
无济于事。
欢迎提出建议!
我想在一个for循环中引用两个向量,但是每个向量的长度都不同。
n=1:50
m=letters[1:14]
Run Code Online (Sandbox Code Playgroud)
我尝试了一个循环来阅读它
for (i in c(11:22,24,25)){
cat (paste(n[i],m[i],sep='\t'),sep='\n')
}
Run Code Online (Sandbox Code Playgroud)
并最终得到:
11 k
12 l
13 m
14 n
15 NA
16 NA
17 NA
18 NA
19 NA
20 NA
21 NA
22 NA
24 NA
25 NA
Run Code Online (Sandbox Code Playgroud)
但我想获得:
11 a
12 b
13 c
...
25 n
Run Code Online (Sandbox Code Playgroud)
有没有办法有一个双变量声明?
for (i in c(11:22,24,25) and j in 1:14){
cat (paste(n[i],m[j],sep='\t'),sep='\n')
}
Run Code Online (Sandbox Code Playgroud)
或类似的东西来获得我想要的结果?
类似于在列表中查找连续数字组和在 R 中查找向量中的连续值,不同之处在于我有一个可能高达 4 的数字序列,即
y=c(4*1:4,24,31,33,39,4*16:20)
> y
[1] 4 8 12 16 24 31 33 39 64 68 72 76 80
Run Code Online (Sandbox Code Playgroud)
我想要一个函数来返回每个由最多 4 个分隔的值序列,所以我得到:
4 8 12 16 # all sep by at most 4
31 33 # all sep by at most 4
64 68 72 76 80 # all sep by at most 4
Run Code Online (Sandbox Code Playgroud)
我试过:
st=c(1,which(diff(y)<5)+1)
en=c(st-1,length(y))
y[st]
[1] 4 8 12 16 33 68 72 76 80
y[en]
[1] 4 …
Run Code Online (Sandbox Code Playgroud)