有一个简单的/一行python等同于R的gsub
功能吗?
strings = c("Important text, !Comment that could be removed", "Other String")
gsub("(,[ ]*!.*)$", "", strings)
# [1] "Important text" "Other String"
Run Code Online (Sandbox Code Playgroud) 有一个简单的/一行python等同于R的grepl
功能吗?
strings = c("aString", "yetAnotherString", "evenAnotherOne")
grepl(pattern = "String", x = strings) #[1] TRUE TRUE FALSE
Run Code Online (Sandbox Code Playgroud) Pandas文档列出了一堆"扩展窗口函数":
http://pandas.pydata.org/pandas-docs/version/0.17.0/api.html#standard-expanding-window-functions
但我无法弄清楚他们从文档中做了什么.
我试图将一些转换应用于数据帧中的所有元素.
使用常规应用函数时,我得到一个矩阵,而不是数据帧.有没有办法直接获取数据帧而不添加as.data.frame
到每一行?
df = data.frame(a = LETTERS[1:5], b = LETTERS[6:10])
apply(df, 1, tolower) #Matrix
apply(df, 2, tolower) #Matrix
sapply(df, tolower) #Matrix
as.data.frame(sapply(df, tolower)) # Can I avoid "as.data.frame"?
Run Code Online (Sandbox Code Playgroud) 任何想法什么是R的蟒蛇的等价物order
?
order(c(10,2,-1, 20), decreasing = F)
# 3 2 1 4
Run Code Online (Sandbox Code Playgroud) do.call
python中是否有相当于R的?
do.call(what = 'sum', args = list(1:10)) #[1] 55
do.call(what = 'mean', args = list(1:10)) #[1] 5.5
?do.call
# Description
# do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.
Run Code Online (Sandbox Code Playgroud) 我正在分析气候模型仿真中的海洋温度数据,其中4D数据数组(时间,深度,纬度,经度;dask_array
如下所示)的形状通常为(6000、31、189、192),大小约为25GB(因此,我渴望使用dask;在尝试使用numpy处理这些数组时,我遇到了内存错误。
我需要沿着时间轴在每个级别/纬度/经度点处拟合三次多项式,并存储所得的4个系数。因此,我已经设置chunksize=(6000, 1, 1, 1)
好了,因此每个网格点都有一个单独的块。
这是我获取三次多项式系数的函数(time_axis
轴值是在其他位置定义的全局一维numpy数组):
def my_polyfit(data):
return numpy.polyfit(data.squeeze(), time_axis, 3)
Run Code Online (Sandbox Code Playgroud)
(因此,在这种情况下,numpy.polyfit
返回长度为4的列表)
这是我认为我需要将其应用于每个块的命令:
dask_array.map_blocks(my_polyfit, chunks=(4, 1, 1, 1), drop_axis=0, new_axis=0).compute()
Run Code Online (Sandbox Code Playgroud)
现在,时间轴消失了(因此drop_axis=0
),并且它所在的地方(长度为4)有了一个新的系数轴。
当我运行此命令时,我得到了IndexError: tuple index out of range
,所以我想知道我在哪里/如何误解了map_blocks
?
我正在寻找一种直接的方法来获取嵌套列表中的第一级元素的唯一类.
可重复的例子:
x = list(list(1,"A"), # Unique classes: character, numeric
4, # Unique classes: numeric
list(1,2,list(4,5, list(6)))) # Unique classes: numeric
# Expected return: list(c('character', 'numeric'), c('numeric'), c('numeric'))
Run Code Online (Sandbox Code Playgroud) 我想读使用CSV data.table
的fread
地方小数点,
。它返回有关更改系统区域设置的错误。
任何想法如何进行?
sampleCSV = "a;b;c
1,03;80,3;50,4
45,2;65,3;90,678"
library(data.table)
fread(sampleCSV, sep = ';', dec = ",", verbose = T)
# *** Verbose & Error message: ***
# dec=',' but current locale ('C') has dec='.'. Attempting to change locale to one that has the desired decimal point.
# Changing to system locale ('en_US.UTF-8') did not provide the desired dec. Now trying any provided in getOption('datatable.fread.dec.locale')
# Trying 'fr_FR.utf8'
# Sys.setlocale('LC_NUMERIC','fr_FR.utf8') returned ""; i.e., this locale name …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将数组写入CSV而不使用行和列名称.将列名设置为false似乎不起作用.我得到"x"作为列名和警告.
任何想法如何删除这个"x"?
write.csv(1:5, row.names = F, col.names = F)
# "x"
# 1
# 2
# 3
# 4
# 5
# Warning message:
# In write.csv(1:5, row.names = F, col.names = F) :
# attempt to set 'col.names' ignored
Run Code Online (Sandbox Code Playgroud) r ×8
python ×5
python-2.7 ×4
apply ×1
csv ×1
dask ×1
data.table ×1
dataframe ×1
list ×1
locale ×1
nested ×1
pandas ×1
ubuntu ×1
ubuntu-16.04 ×1