我看到很多类似 R 的问题,但我找不到专门针对 Python 的问题,最好使用 numpy。
假设我有一组观察结果存储在x
. q * 100
我可以获得人口累计百分比的值。
# Import numpy
import numpy as np
# Get 75th percentile
np.quantile(a=x, q=0.75)
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否有一个函数可以实现相反的功能。也就是说,一个 numpy 函数将一个值作为输入并返回q
。
为了进一步扩展这一点,scipy 分发对象有一个ppf
方法可以让我做到这一点。我正在寻找 numpy 中类似的东西。它存在吗?
我有一个简单的语法问题:有没有办法指定在.csv
函数本身中写入 csv 文件的路径?
我总是做以下事情:
setwd("C:/Users/user/Desktop")
write.csv(dt, "my_file.csv", row.names = F)
Run Code Online (Sandbox Code Playgroud)
但是,我想跳过该setwd()
行并将其直接包含在write.csv()
函数中。我在write.csv
文档文件中找不到路径设置。是否可以在write.csv
不使用write.table()
或无需下载任何软件包的情况下专门执行此操作?
我.csv
在每天自动运行的脚本中编写了大约 300 个文件。使用write.table()
时循环运行速度比使用时慢write.csv()
。我想在write.csv()
函数中包含路径的全部原因是看看我是否可以减少进一步执行所需的时间。
我正在尝试从存储在 S3 中的 csv 文件在 AWS Athena 中创建外部表。
csv 文件如下所示。正如您所看到的,数据没有用引号 ( "
) 括起来,而是用逗号 ( ) 分隔,
。
ID,PERSON_ID,DATECOL,GMAT
612766604,54723367,2020-01-15,637
615921503,158634997,2020-01-25,607
610656030,90359154,2020-01-07,670
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码来创建表:
ID,PERSON_ID,DATECOL,GMAT
612766604,54723367,2020-01-15,637
615921503,158634997,2020-01-25,607
610656030,90359154,2020-01-07,670
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下代码预览表格:
CREATE EXTERNAL TABLE my_table
(
ID string,
PERSON_ID int,
DATE_COL date,
GMAT int
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
STORED AS TEXTFILE
LOCATION 's3://my_bucket/som_bucket/dat/'
TBLPROPERTIES
(
'skip.header.line.count'='1'
)
;
Run Code Online (Sandbox Code Playgroud)
这会引发此错误:
HIVE_BAD_DATA:解析字段 2 的字段值“2020-01-15”时出错:对于输入字符串:“2020-01-15”
我的问题是:我是否传递了正确的 serde?如果是这样,如何设置日期列 ( DATE_COL
) 的格式,使其以 YYYY-MM-DD 格式读取和显示日期?
我将以下面板存储在df
:
状态 | 区 | 年 | y | 持续的 | x1 | x2 | 时间 | |
---|---|---|---|---|---|---|---|---|
0 | 01 | 01001 | 2009年 | 12 | 1 | 0.956007 | 639673 | 1 |
1 | 01 | 01001 | 2010年 | 20 | 1 | 0.972175 | 639673 | 2 |
2 | 01 | 01001 | 2011年 | 22 | 1 | 0.988343 | 639673 | 3 |
3 | 01 | 01002 | 2009年 | 0 | 1 | 0 | 33746 | 1 |
4 | 01 | 01002 | 2010年 | 1 | 1 | 0.225071 | 33746 | 2 |
5 | 01 | 01002 | 2011年 | 5 | 1 | 0.450142 | 33746 | 3 |
6 | 01 | 01003 | 2009年 | 0 | 1 | 0 | 45196 | 1 |
7 | 01 | 01003 | 2010年 | 5 | 1 … |
我想知道 Jupyter Notebook 是否有一个命令,可以在发出命令后停止后续单元格的运行。
I found this question and tried sys.exit()
but some of the cells below the command are still being executed.
The structure of my script looks as follows:
# tons of cells above
Run Code Online (Sandbox Code Playgroud)
if df["target"].mean() == 1:
sys.exit("Condition met. Do not execute the cells below.")
Run Code Online (Sandbox Code Playgroud)
# tons of cells below
Run Code Online (Sandbox Code Playgroud)
Ideally, none of the cells represented by tons of cells below
should be executed if df["target"].mean()
is 1
. Otherwise, I do want to the subsequent cells …
我正在尝试使用 Google Colab 上的 gdf.overlay 函数与 2 个 GeoDataFrame 进行交集。请看下面的代码
!sudo apt install libspatialindex-dev
!sudo pip3 install rtree
!pip install pygeos
overlap = gpd.overlay(gdf1,gdf2, how='intersection')
overlap.plot(figsize=(10,10), cmap='jet')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息
ImportError: Spatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.html
Run Code Online (Sandbox Code Playgroud)
是什么导致了这里的问题?
我正在尝试在 VS Code 中运行 Jupyter Notebook。但是,每当我尝试执行单元格时,我都会收到以下错误消息:
Failed to start the Kernel.
Jupyter server crashed. Unable to connect.
Error code from Jupyter: 1
usage: jupyter.py [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]
[--paths] [--json] [--debug]
[subcommand]
Jupyter: Interactive Computing
positional arguments:
subcommand the subcommand to launch
options:
-h, --help show this help message and exit
--version show the versions of core jupyter packages and exit
--config-dir show Jupyter config dir
--data-dir show Jupyter data dir
--runtime-dir show Jupyter runtime dir
--paths show all …
Run Code Online (Sandbox Code Playgroud) df
是一个pandas数据框。该列df["Date"]
是日期时间字段。
test_date = df.loc[300, "Date"] # Timestamp('2019-02-12 00:00:00')
Run Code Online (Sandbox Code Playgroud)
我想把它重置回第一天。我试过:
test_date.day = 1 # Attribute 'day' of 'datetime.date' objects is not writable
Run Code Online (Sandbox Code Playgroud)
如何将其重置回第一天(最好不加载其他库)?
这篇文章与我想做的类似,但它使用 Python 2 和旧版本的 pandas。
我在名为 的对象的列中有很多分数example
。我想将这些分数分成十分位数,并将相应的十分位数间隔分配给每一行。我尝试了以下方法:
import random
import pandas as pd
random.seed(420) #blazeit
example = pd.DataFrame({"Score":[random.randrange(350, 1000) for i in range(1000)]})
example["Decile"] = pd.qcut(example["Score"], 10, labels=False) + 1 # Deciles as integer from 1 to 10
example["Decile_interval"] = pd.qcut(example["Score"], 10) # Decile as interval
Run Code Online (Sandbox Code Playgroud)
这给了我我正在寻找的十分位数。但是,我希望其中的十分位数example["Decile_interval"]
是整数,而不是浮点数。我尝试过precision=0
,但它只显示.0
在每个数字的末尾。
如何将区间中的浮点数转换为整数?
编辑:正如@ALollz 指出的那样,这样做将改变十分位分布。不过,我这样做是为了演示目的,所以我并不担心这一点。支持@JuanC 实现这一点并发布一个解决方案。
我需要从pyearth
Google Colab 上的库加载多元自适应回归样条 (MARS) 算法。这就是我想做的:
# Import model from library
from pyearth import Earth
# Initialize model
reg = Earth()
Run Code Online (Sandbox Code Playgroud)
但是,Google Colab 默认情况下没有该库。当我尝试时收到以下错误提示import pyearth
:
ModuleNotFoundError:没有名为“pyearth”的模块
因此,我尝试使用安装它!pip
,但是,如下所示,它也不起作用。
# Instal `pyearth`
!pip install pyearth # Runs smoothly
# Import Earth
from pyearth import Earth
> ImportError: cannot import name 'Earth' from 'pyearth' (/usr/local/lib/python3.7/dist-packages/pyearth/__init__.py)
Run Code Online (Sandbox Code Playgroud)
奇怪的是,确实import pyearth
有效。
这篇文章解决了一个非常相似的问题,但仍未解决。唯一可用的答案对我不起作用。
python ×8
pandas ×2
quantile ×2
amazon-s3 ×1
datetime ×1
geopandas ×1
linearmodels ×1
numpy ×1
r ×1
regression ×1
statistics ×1
statsmodels ×1