2023年8月31日

Python: Numpy numpy.nonzero 获取非零元素的下标

numpy.nonzero https://numpy.org/doc/stable/reference/generated/numpy.nonzero.html#numpy.nonzero numpy.nonzero(a)[source] Return the indices of the elements that are non-zero. Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The values in a are always tested and returned in row-major, C-style order. To group the indices by element, rathe…
2023年8月31日

Python: Numpy 整数数组索引

Integer array indexing https://numpy.org/doc/stable/user/basics.indexing.html#integer-array-indexing >>>y = np.arange(35).reshape(5, 7) >>>y array([[ 0, 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], [28, 29, 30, 31, 32, 33, 34]]) >>>y[np.array([0, 2, 4]), np.array([0, 1, 2])] array([ 0, 15, 30]) In this case, if the index arrays have…
2023年8月30日

Python: Python map() 函数

Python map() 函数 描述 map() 会根据提供的函数对指定序列做映射。 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。 语法 map() 函数语法: map(function, iterable, ...) 参数 function -- 函数 iterable -- 一个或多个序列 返回值 Python 2.x 返回列表。 Python 3.x 返回迭代器。 实例 以下实例展示了 map() 的使用方法: Python2.x 实例 >>> def square(x) :            # 计算平方数 ...     return x ** 2 ... >>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方 [1, 4, 9, 16, 25] >…
2023年8月30日

Python: Numpy 随机数生成及Numpy 1.1.7版本后 随机数生成新方法

目录 Random sampling Random Generator: random.Generator 创建 使用 1、创建指定维度数组 2、创建随机一维整数 3、随机选择 4、随机排列 分布 Legacy Random Generation: random.RandomState Simple random data Random sampling 从Numpy 1.17开始,Generator代替RandomState,官方文档: https://numpy.org/doc/stable/reference/random/index.html Random Generator https://numpy.org/doc/stable/reference/random/generator.html#random-generator 创建 随机数的产生需要先创建一…
2023年8月30日

Python: Pandas 比较 pandas.DataFrame.where 和 numpy.where

pandas.DataFrame.where https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.where.html?pandas.DataFrame.where DataFrame.where(cond, other=_NoDefault.no_default, *, inplace=False, axis=None, level=None)[source] Replace values where the condition is False. Parameters: cond: bool Series/DataFrame, array-like, or callable Where cond is True, keep the original value. Where False, repla…
2023年8月29日

Python: Pandas 使用 series[] / frame[] 选择和切片

series[] / frame[]有两种用法,返回不同的类型: 1. series[索引名字] / frame[列名字] 2. series[::] / frame[::] 具体如下(参见:https://pandas.pydata.org/docs/user_guide/indexing.html): Basics As mentioned when introducing the data structures in the last section, the primary function of indexing with [] (a.k.a. __getitem__ for those familiar with implementing class behavior in Python) is selecting out lower-dimensional slices. The follo…
2023年8月29日

Python: Pandas 比较 pandas.Series.isin 和 pandas.Series.reindex

In [283]: s = pd.Series(np.arange(5), index=np.arange(5)[::-1], dtype='int64') s Out[283]: 4 0 3 1 2 2 1 3 0 4 dtype: int64 In [284]: s.isin([2, 4, 6]) Out[284]: 4 False 3 False 2 True 1 False 0 True dtype: bool In [285]: s[s.isin([2, 4, 6])] Out[285]: 2 2 0 4 dtype: int64 In [286]: s[s.index.isin([2, 4, 6])] Out[286]: 4 0 2 2 dtype: int64 In [287]: s.reindex([2, 4, 6]) Out[287]: 2 2.0 4 0.0 6 NaN dtype: float64…
2023年8月29日

Python: Pandas 按可调用对象选择数据 / 筛选数据

1. 概述: Indexing and selecting data Selection by callable .loc, .iloc, and also [] indexing can accept a callable as indexer. The callable must be a function with one argument (the calling Series or DataFrame) that returns valid output for indexing. df1 = pd.DataFrame(np.random.randn(6, 4), index=list('abcdef'), columns=list('ABCD')) df1 Out[90]: A B C D a -0.023688 2.410179 1.450520 0.206053 b -0.251905 -2.213588 1.06…
2023年8月26日

Python: Python查看包的版本

当你需要更新或安装Python包时,可能需要查看当前环境下某个包的版本信息。Python提供了多种方法可以查看包的版本。 一、使用pip查看版本 pip是Python的包管理器,可以通过命令行安装、卸载、更新包。使用pip可以非常方便地查看包的版本信息。 首先,打开命令行终端。输入以下命令: pip show packagename 这里的packagename是你要查看版本的包名。例如,想查看numpy这个包的版本,可以输入以下命令: pip show numpy 输出结果如下: Name: numpy Version: 1.18.5 Summary: NumPy is the fundamental package for array computing with Python. 可以看到,numpy的版本是1.18.5。 二、使用conda查看版本 conda也…
2023年8月26日

Python: pandas_datareader 获取 Yahoo Finance Data 报错 RemoteDataError: Unable to read URL / TypeError: string indices must be integers

1. 问题概述: pandas_datareader 获取 Yahoo Finance Data: import pandas_datareader.data as web df = web.get_data_yahoo('GE', start='2019-09-10', end='2019-10-09') # 或 df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09') 报错1: RemoteDataError: Unable to read URL: https://finance.yahoo.com/quote/GE/history?period1=1568059200&period2=1570651199&interval=1d&frequency=1…