我有一个包含各种列的数据框,某些列中的某些数据包含双引号,我想删除它们,例如:
ID name value1 value2
"1 x a,"b,"c x"
"2 y d,"r" z"
Run Code Online (Sandbox Code Playgroud)
我希望这看起来像这样:
ID name value1 value2
1 x a,b,c x
2 y d,r z
Run Code Online (Sandbox Code Playgroud) 我有以下数据框让我们称之为df
id type company
1 NA NA
2 NA ADM
3 North Alex
4 South NA
NA North BDA
6 NA CA
Run Code Online (Sandbox Code Playgroud)
我想只保留"类型"和"公司"栏中没有NA的记录
id type company
3 North Alex
NA North BDA
Run Code Online (Sandbox Code Playgroud)
我累了
df_non_na <- df[!is.na(df$company) || !is.na(df$type), ]
Run Code Online (Sandbox Code Playgroud)
但这没效果.
提前致谢
我想下载所有的.xls
或.xlsx
或.csv
从本网站到一个指定的文件夹.
https://www.rbi.org.in/Scripts/bs_viewcontent.aspx?Id=2009
Run Code Online (Sandbox Code Playgroud)
我已经研究过机械化,漂亮的汤,urllib2等.Mechanize在Python 3中不起作用,urllib2也有Python 3的问题,我寻找解决方法,但我不能.所以,我目前正在尝试使用Beautiful Soup工作.
我找到了一些示例代码并尝试修改它以适应我的问题,如下所示 -
from bs4 import BeautifulSoup
# Python 3.x
from urllib.request import urlopen, urlretrieve, quote
from urllib.parse import urljoin
url = 'https://www.rbi.org.in/Scripts/bs_viewcontent.aspx?Id=2009/'
u = urlopen(url)
try:
html = u.read().decode('utf-8')
finally:
u.close()
soup = BeautifulSoup(html)
for link in soup.select('div[webpartid] a'):
href = link.get('href')
if href.startswith('javascript:'):
continue
filename = href.rsplit('/', 1)[-1]
href = urljoin(url, quote(href))
try:
urlretrieve(href, filename)
except:
print('failed to download')
Run Code Online (Sandbox Code Playgroud)
但是,运行时此代码不会从目标页面中提取文件,也不会输出任何失败消息(例如"无法下载").
我有一个数据帧df,我正在构建一个机器学习模型(C5.0决策树)来预测列的类(loan_approved):
结构(不是真实数据):
id occupation income loan_approved
1 business 4214214 yes
2 business 32134 yes
3 business 43255 no
4 sailor 5642 yes
5 teacher 53335 no
6 teacher 6342 no
Run Code Online (Sandbox Code Playgroud)
处理:
功能:
error_free_predict = function(x){
output = tryCatch({
predict(C50_model, newdata = test[x,], type = "class")
}, error = function(e) {
"no"
})
return(output)
}
Run Code Online (Sandbox Code Playgroud)
应用预测功能:
test <- mutate(test, predicted_class = error_free_predict(1:NROW(test)))
Run Code Online (Sandbox Code Playgroud)
问题:
id occupation income loan_approved predicted_class
1 business 4214214 yes no
2 business 32134 yes no
3 …
Run Code Online (Sandbox Code Playgroud) 我在R中有一个脚本,白天经常被其他脚本调用。我在终端中使用
Rscript code.R
Run Code Online (Sandbox Code Playgroud)
我注意到加载程序包和设置R需要很多时间。
是否可以将R作为后台服务运行,而我是使用端口或其他工具访问的?
我有以下数据框(df),尽管我认为我无法解决如何执行以下操作:
输入:
id business_id type date1 date2 date3
1 A1 Month 13/10/13 13/09/13 13/08/13
1 A1 Total Net Deposits 1500 951 190
1 A1 Month end Bank Balance 729 650 164
Run Code Online (Sandbox Code Playgroud)
预期产出:
id business_id Month Total Net Deposits Month end Bank Balance
1 A1 13/10/13 1500 729
1 A1 13/09/13 951 650
1 A1 13/09/13 190 164
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,其结构和长度可以保持变化,即
输入:
X <- ("A=12&B=15&C=15")
Y <- ("A=12&B=15&C=15&D=32&E=53")
Run Code Online (Sandbox Code Playgroud)
我正在寻找这个字符串转换为数据框架
产量预期:
数据帧X.
A B C
12 15 15
Run Code Online (Sandbox Code Playgroud)
和Dataframe Y.
A B C D E
12 15 15 32 53
Run Code Online (Sandbox Code Playgroud)
我厌倦了这个:
X <- as.data.frame(strsplit(X, split="&"))
Run Code Online (Sandbox Code Playgroud)
但这对我来说不起作用,因为它只创建了一个列和列名称搞砸了.
PS:我不能硬编码列名,因为它们可以变化,并且在任何给定时间,字符串将只包含一行
我有一个火花的df,如下结构:
amount gender status
1000 male married
1313 female single
1000 male married
Run Code Online (Sandbox Code Playgroud)
基本上我想创建一个性别是数字的新列
amount gender status gender_num
1000 male married 1
1313 female single 2
1000 male married 1
Run Code Online (Sandbox Code Playgroud)
我厌倦了以下几点:
val gender = df.gender
val gender_num = gender match {
case male => 1
case female => 2
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
<console>:125: error: value pa_gender_category is not a member of org.apache.spark.sql.DataFrame
val gender = data.pa_gender_category
Run Code Online (Sandbox Code Playgroud)
我知道有一个stringtoindex函数,但我想手动执行此操作
从以下数据帧 df1 可以看出
Branch Loan_Amount TAT
A 100 2.0
A 120 4.0
A 300 9.0
B 150 1.5
B 200 2.0
Run Code Online (Sandbox Code Playgroud)
我可以使用聚合函数将以下输出作为数据帧 df2 获取
Branch Number_of_loans Loan_Amount Total_TAT
A 3 520 15.0
B 2 350 3.5
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用 nrow 来计算 number_of_loans 并合并,但我正在寻找更好的方法。
我正在使用包探索 R 中的旅行商问题TSP
,一切正常,但我唯一的问题是情节中城市的名称没有出现。
基本上在最后一行代码中,我想将行名作为标签
代码:
library(TSP)
set.seed(123)
x <- data.frame(x = runif(20), y = runif(20), row.names = LETTERS[1:20])
## create a TSP
etsp <- ETSP(x)
etsp
## use some methods
n_of_cities(etsp)
labels(etsp)
## plot ETSP and solution
tour <- solve_TSP(etsp)
tour
plot(etsp, tour, tour_col = "red")
Run Code Online (Sandbox Code Playgroud)