我尝试在 Windows 10 系统中安装或更新新软件包,其中安装了 Anaconda3(2019 版)。但每次我都会遇到相同的 SSL 错误。如果我可以在家庭 WiFi 网络中访问该网络,我会怀疑这可能是公司防火墙问题。但到处我都会遇到同样的错误。运行 COnda 搜索时,出现此错误:
conda search
Loading channels: failed
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/free/win-64/repodata.json.bz2>
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.
SSLError(MaxRetryError('HTTPSConnectionPool(host=\'repo.anaconda.com\', port=443): Max retries exceeded with url: /pkgs/free/win-64/repodata.json.bz2 (Caused by SSLError("Can\'t …
Run Code Online (Sandbox Code Playgroud) 举个简单的例子,考虑arr
如下定义的numpy数组:
import numpy as np
arr = np.array([[5, np.nan, np.nan, 7, 2],
[3, np.nan, 1, 8, np.nan],
[4, 9, 6, np.nan, np.nan]])
Run Code Online (Sandbox Code Playgroud)
arr
在控制台输出中看起来像这样:
array([[ 5., nan, nan, 7., 2.],
[ 3., nan, 1., 8., nan],
[ 4., 9., 6., nan, nan]])
Run Code Online (Sandbox Code Playgroud)
我现在想逐行'向前填充' nan
数组中的值arr
.我的意思是用nan
左边最近的有效值替换每个值.期望的结果如下所示:
array([[ 5., 5., 5., 7., 2.],
[ 3., 3., 1., 8., 8.],
[ 4., 9., 6., 6., 6.]])
Run Code Online (Sandbox Code Playgroud)
我尝试过使用for循环:
for row_idx in range(arr.shape[0]):
for col_idx in range(arr.shape[1]): …
Run Code Online (Sandbox Code Playgroud) 为了举例,假设我有一个函数,它将两个numpy数组作为输入参数.第一个数组必须是2维的,并且只包含浮点数.第二个数组必须是1维的,并且只包含布尔值.
到目前为止,我还没有真正找到在docstring中指定输入数组数据类型和维度的现有约定.我想到的一种可能的格式(以numpy docstring约定为基础)是这样的:
def example_function(arr1, arr2):
"""This is an example function.
Parameters
----------
arr1 : ndarray(dtype=float, ndim=2)
Array containing some kind of data.
arr2 : ndarray(dtype=bool, ndim=1)
Array containing some kind of mask.
"""
Run Code Online (Sandbox Code Playgroud)
这可以被认为是"正确的"文档字符串格式吗?(即它是否违反了现有docstring约定的任何规则?)
我必须从类似 CSV 的文件创建一个 pandas 数据框,该文件具有以下特征:
#
.我尝试使用pd.read_csv
带有参数sep=None
和 的方法来解决这个问题comment='#'
。据我了解,该sep=None
参数告诉 pandas 自动检测分隔符,并且该comment='#'
参数告诉 pandas 所有以 开头的行#
都是应该被忽略的注释行。
这些参数单独使用时效果很好。但是,当我同时使用它们时,我收到错误消息TypeError: expected string or bytes-like object
。以下代码示例演示了这一点:
from io import StringIO
import pandas as pd
# Simulated data file contents
tabular_data = (
'# Data generated on 04 May 2017\n'
'col1,col2,col3\n'
'5.9,7.8,3.2\n'
'7.1,0.4,8.1\n'
'9.4,5.4,1.9\n'
)
# This works
df1 = pd.read_csv(StringIO(tabular_data), sep=None)
print(df1)
# This also works
df2 = …
Run Code Online (Sandbox Code Playgroud) 我有一个 pandas 数据框定义如下:
import pandas as pd
headers = ['Group', 'Element', 'Case', 'Score', 'Evaluation']
data = [
['A', 1, 'x', 1.40, 0.59],
['A', 1, 'y', 9.19, 0.52],
['A', 2, 'x', 8.82, 0.80],
['A', 2, 'y', 7.18, 0.41],
['B', 1, 'x', 1.38, 0.22],
['B', 1, 'y', 7.14, 0.10],
['B', 2, 'x', 9.12, 0.28],
['B', 2, 'y', 4.11, 0.97],
]
df = pd.DataFrame(data, columns=headers)
Run Code Online (Sandbox Code Playgroud)
在控制台输出中看起来像这样:
Group Element Case Score Evaluation
0 A 1 x 1.40 0.59
1 A 1 y 9.19 …
Run Code Online (Sandbox Code Playgroud) 考虑以下两个数据帧:
import pandas as pd # version 0.23.4
df1 = pd.DataFrame({
'A': [1, 1, 1, 2, 2],
'B': [100, 100, 200, 100, 100],
'C': ['apple', 'orange', 'mango', 'mango', 'orange'],
'D': ['jupiter', 'mercury', 'mars', 'venus', 'venus'],
})
df2 = df1.astype({'D': 'category'})
Run Code Online (Sandbox Code Playgroud)
正如您在数据框中看到的那样,df2
该列 D
是分类数据类型,但在其他方面df2
是相同的df1
.
现在考虑以下groupby-aggregation操作:
result_x_df1 = df1.groupby(by='A').first()
result_x_df2 = df2.groupby(by='A').first()
result_y_df1 = df1.groupby(by=['A', 'B']).first()
result_y_df2 = df2.groupby(by=['A', 'B']).first()
Run Code Online (Sandbox Code Playgroud)
结果如下:
In [1]: result_x_df1
Out[1]:
B C D
A
1 100 apple jupiter …
Run Code Online (Sandbox Code Playgroud) 我有一个包含一些nan
值的2D Numpy数组.简化示例:
arr = np.array([[3, 5, np.nan, 2, 4],
[9, 1, 3, 5, 1],
[8, np.nan, 3, np.nan, 7]])
Run Code Online (Sandbox Code Playgroud)
在控制台输出中看起来像这样:
array([[ 3., 5., nan, 2., 4.],
[ 9., 1., 3., 5., 1.],
[ 8., nan, 3., nan, 7.]])
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种将所有值设置在现有nan
值右侧的nan
好方法.换句话说,我需要将示例数组转换为:
array([[ 3., 5., nan, nan, nan],
[ 9., 1., 3., 5., 1.],
[ 8., nan, nan, nan, nan]])
Run Code Online (Sandbox Code Playgroud)
我知道如何使用循环实现这一点,但我认为只使用Numpy矢量化操作的方法会更有效.有没有人可以帮我找到这样的方法?
我是Python的新手,正在从事矩阵的转置工作,但无论任何短程序,我都发现它冗长的代码!
mymatrix=[(1,2,3),(4,5,6),(7,8,9),(10,11,12)]
for myrow in mymatrix:
print(myrow)
print("\n")
t_matrix = zip(*mymatrix)
for myrow in t_matrix:
print(myrow)
Run Code Online (Sandbox Code Playgroud) 举个简单的例子,考虑以下pandas数据帧:
import pandas as pd
headers = ["city", "year", "births", "deaths", "immigrations", "emigrations"]
data = [
["Gotham", 2016, 1616, 1020, 1541, 1893],
["Gotham", 2015, 1785, 1708, 1604, 1776],
["Gotham", 2014, 1279, 1946, 1991, 1169],
["Gotham", 2013, 1442, 1932, 1960, 1580],
["Metropolis", 2016, 6405, 6393, 5390, 6797],
["Metropolis", 2015, 6017, 5492, 5647, 6994],
["Metropolis", 2014, 6644, 6893, 6759, 5149],
["Metropolis", 2013, 6902, 6160, 5294, 5112],
["Smallville", 2016, 43, 10, 29, 48],
["Smallville", 2015, 16, 21, 17, 19],
["Smallville", 2014, 20, …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种有效的方法来识别pandas
Index
对象中连续重复相同值的所有子范围.
举个简单的例子,考虑以下pandas
Index
对象:
import pandas as pd
idx = pd.Index(['X', 'C', 'C', 'C', 'Q', 'Q', 'Q', 'Q', 'A', 'P', 'P'])
Run Code Online (Sandbox Code Playgroud)
在这个例子中,值C
从位置1到3 Q
重复,值从位置4到7 P
重复,值从位置9到10重复.然后我试图得到的结果是元组列表(或类似的东西)像这样:
[(1, 3, 'C'), (4, 7, 'Q'), (9, 10, 'P')]
Run Code Online (Sandbox Code Playgroud)
我一直在试验这个pandas.Index.duplicated
房产,但仅凭这一点,我还没有成功地取得理想的结果.
非常感谢大家的回答.我有一个后续问题.假设Index
它还包含非连续的重复值,如本示例所示(其中value X
出现多次):
idx = pd.Index(['X', 'C', 'C', 'C', 'Q', 'Q', 'Q', 'Q', 'X', 'P', 'P'])
Run Code Online (Sandbox Code Playgroud)
你怎么能得到一个忽略这些X
值的结果?即如何获得此示例的以下结果:
[(1, 3, 'C'), (4, 7, 'Q'), (9, 10, 'P')]
Run Code Online (Sandbox Code Playgroud) 我对 python 比较陌生,并尝试使用 pip 在 python 3.7 上安装 geopandas。出于不同的原因,我想避免使用 anaconda 发行版。在这篇文章之后,我能够通过首先手动安装依赖项来成功安装 geopandas。问题是现在我在尝试导入 geopandas 时遇到了一个问题:
import geopandas
Run Code Online (Sandbox Code Playgroud)
随后的错误消息是:
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\geopandas\__init__.py", line 5, in <module>
from geopandas.io.file import read_file
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\geopandas\io\file.py", line 4, in <module>
import fiona
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fiona\__init__.py", line 87, in <module>
from fiona.collection import BytesCollection, Collection
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fiona\collection.py", line 9, in <module>
from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
ImportError: DLL load failed: The specified module could not be found.
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激
我正在学习 Pandas 中的管道和功能联合。我了解管道的工作,这有助于将一系列转换应用于给定的数据集。但是,我对功能联合感到困惑。我已经阅读了文档,其中说变压器是并行应用的,然后将结果连接起来。
我对此有疑问,如果我们将所有转换器应用于整个数据集或不同的转换器仅应用于选定的特征?如果到整个数据集,我们如何连接结果?另外,是否有任何我应该使用 FeatureUnion 的一般用例?
我有两个清单
subject_id = [10030, 10030, 10250, 10510]
student_id = [55000, 55804, 55804, 55000]
Run Code Online (Sandbox Code Playgroud)
我想创建一个以 subject_id 作为键、以 Student_id 作为值的字典。当我尝试时dict
,zip
由于重复的键值,我只得到第二个值,而不是第一个值。
result = {10030: 55804, 10250: 55804, 10510: 55000}
Run Code Online (Sandbox Code Playgroud)
我的想法是让值成为一个列表。因此,当重复键出现时,相应的student_id将作为新元素添加到列表中。
期望的结果:
my_dict = {10030: [55000, 55804], 10250: [55804], 10510: [55000]}
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?先感谢您。
python ×13
pandas ×7
numpy ×3
aggregate ×2
arrays ×2
dataframe ×2
performance ×2
anaconda ×1
categories ×1
conda ×1
csv ×1
dictionary ×1
duplicates ×1
geopandas ×1
list ×1
matrix ×1
pip ×1
pipeline ×1
scikit-learn ×1
transpose ×1
windows ×1