我有一个包含几列(功能)的数据框。
>>> print(df)
col1 col2
a 1 1
b 2 2
c 3 3
d 3 2
Run Code Online (Sandbox Code Playgroud)
我想计算其中之一的模式。这是发生的事情:
>>> print(df['col1'].mode())
0 3
dtype: int64
Run Code Online (Sandbox Code Playgroud)
我想简单地输出 value 3
。这种行为很奇怪,如果您认为以下非常相似的代码正在运行:
>>> print(df['col1'].mean())
2.25
Run Code Online (Sandbox Code Playgroud)
所以有两个问题:为什么会发生这种情况?我怎样才能获得纯模式值,因为它发生在平均值上?
我是一名 Python 新手,正在研究对象编程。研究我的老师代码,我发现了一些奇怪的东西self.items = list(items)
。看一下整体代码:
class Truck():
'''
This class models a truck
'''
def __init__(self, plate_number, capacity, items):
'''
Constructor of an object of class Truck
:param plate_number: a unique number Identifying the truck
:param capacity: maximum weight that the truck can carry
:param items: list of objects of class Item carried by the truck
'''
self.plate_number = plate_number
self.capacity = capacity
self.items = list(items)
Run Code Online (Sandbox Code Playgroud)
那么,为什么我要强制转换该参数items
,而其他参数不需要这个操作呢?
我有一个具有常规表单的String文本,并希望获取此String的两个部分.String具有格式
"<html><div style=\"text-align:center;\"><b>****</b><br><i>Aula: </i><b>****</b></div></html>"
Run Code Online (Sandbox Code Playgroud)
其中****
指示我想要的字符串部分.我能怎么做?我正在使用JAVA,字符串也是用HTML编写的.
我们可以看到String的有趣部分都受到<b>
和的限制<\b>
我已经删除了熊猫DataFrame中的一些行,但是在新的DataFrame中,索引未刷新,即从此刷新:
id marks
1 123 45
2 124 67
3 127 89
4 257 10
5 345 34
Run Code Online (Sandbox Code Playgroud)
我已经获得:
id marks
2 124 67
4 257 10
5 345 34
Run Code Online (Sandbox Code Playgroud)
当我想要时:
id marks
1 124 67
2 257 10
3 345 34
Run Code Online (Sandbox Code Playgroud)