小编Bub*_*Gut的帖子

在Python中为内置函数赋予别名的目的是什么?

我正在阅读Python headq.merge的代码,看起来他们正在为内置函数创建别名_len = len.只是想知道它的目的是什么?

非常感谢!

python

7
推荐指数
1
解决办法
100
查看次数

DataFrame.set_index返回'str'对象不可调用

找到解决方法后,我不在这里寻找解决方案;大多数情况下,我只是想了解为什么原来的方法不起作用,而周围的方法却起作用了。

我有一个带有默认数字键的2803行的数据框。我想将其替换为列0中的值,即TKR。

所以我使用f.set_index('TKR')并得到

f.set_index('TKR')

Traceback (most recent call last):

  File "<ipython-input-4-39232ca70c3d>", line 1, in <module>
    f.set_index('TKR')

TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)

因此,我认为TKR列中可能会有一些干扰,而不是我尝试滚动2803行 f.head().set_index('TKR')

什么时候可行,我尝试f.head(100).set_index('TKR')也可行。我继续使用500、1000和1500的所有参数。2800、2801、2802和2803也是如此。

f.head(len(f)).set_index('TKR')

该方法有效,并将在下个月处理其他大小的数据框。我只想了解为什么这样做有效,而按book方法进行的原始,简单和(我认为)无效。

我在Windows 10机器上使用Python 3.6(64位)和Pandas 0.18.1

python pandas

6
推荐指数
2
解决办法
737
查看次数

我如何列出目录中的文件夹

这是我的代码:

import os

def get_file():
    files = os.listdir('F:/Python/PAMC')
    print(files)

    for file in files:
        print(file)

get_file()
Run Code Online (Sandbox Code Playgroud)

我如何只列出python目录中的文件夹?

python python-2.7 python-3.x

5
推荐指数
2
解决办法
1万
查看次数

Python特殊方法名称的完整列表?

我想知道是否有办法以编程方式列出所有特殊方法(如__getattr__,__lt__),而不是转到Python数据模型文档并复制粘贴所有这些方法.因为我可以dir(builtins)用来检索内置标识符列表,所以我希望还有一种特殊方法的方法.

python python-3.x

5
推荐指数
1
解决办法
284
查看次数

在这里覆盖__new__的目的是什么?

我正在阅读Python intervaltree模块的代码并看到以下代码,(源代码在这里):

class Interval(namedtuple('IntervalBase', ['begin', 'end', 'data'])):
    __slots__ = ()  # Saves memory, avoiding the need to create __dict__ for each interval

    def __new__(cls, begin, end, data=None):
        return super(Interval, cls).__new__(cls, begin, end, data)
Run Code Online (Sandbox Code Playgroud)

它似乎没有在__new__函数中做任何额外的事情,那么在这里覆盖它的目的是什么?

任何帮助将非常感谢!谢谢!

python

4
推荐指数
1
解决办法
62
查看次数

从字典创建Pandas DataFrame

我试图将字典转换{'a': [1, 2, 3], 'b': [4, 5]}为:

 0           | 1           | 
|:-----------|------------:| 
| a          |        1    |  
| a          |        2    |   
| a          |        3    |     
| b          |        4    |    
| b          |        5    |     
Run Code Online (Sandbox Code Playgroud)

但是没有找到任何方法来实现这一点而不修改字典本身.有更简单的方法吗?任何帮助将非常感谢!谢谢!

python dictionary dataframe python-3.x pandas

4
推荐指数
1
解决办法
905
查看次数

更改numpy数组的dtype会影响数据

虽然我正在玩numpy,

x = np.arange(9).reshape((3,3))
x
Run Code Online (Sandbox Code Playgroud)

它返回 array([[0, 1, 2],[3, 4, 5],[6, 7, 8]])

但是一旦我改变了数据类型

x.dtype=np.int8
x
Run Code Online (Sandbox Code Playgroud)

现在x成为array([[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0,0, 0],[3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0,0, 0],[6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, …

python arrays numpy

2
推荐指数
1
解决办法
141
查看次数

Spark数据帧计算行式最小值

我试图将几列的最小值放入单独的列中。(创建min列)。操作非常简单,但我无法找到正确的函数:
AB min
1 2 1
2 1 1
3 1 1
1 4 1

非常感谢你的帮助!

apache-spark apache-spark-sql

2
推荐指数
1
解决办法
3605
查看次数