- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是尝试对我的数据框进行排序并使用了以下函数:
df[df.count >= df.count.quantile(.95)]
返回错误:
AttributeError: 'function' object has no attribute 'quantile'
但是将系列括起来效果很好:
df[df['count'] >= df['count'].quantile(.95)]
这不是我第一次根据这种区别得到不同的结果,但它通常也不会发生,而且我一直认为这是两个相同的对象。
为什么会这样?
最佳答案
因为 count
是数据框的内置方法之一,当您使用 .
时,它被识别为方法而不是列 count ,即 。
内置方法优先于列:
df = pd.DataFrame({
'A':[1,2,3],
'B':[2,3,4],
'count': [4,5,6]
})
df.count()
#A 3
#B 3
#count 3
#dtype: int64
df.count
# V V V V V V V V
#<bound method DataFrame.count of A B count
#0 1 2 4
#1 2 3 5
#2 3 4 6>
点和括号之间的另一个区别是,您不能使用点来创建新列。即如果该列不存在,df.column = ...
将不起作用,在这种情况下您必须使用方括号。作为 df[column] = ...
,使用上面的虚拟数据框:
# original data frame
df
# A B count
#0 1 2 4
#1 2 3 5
#2 3 4 6
使用点创建新列将不起作用,C 被设置为属性而不是列:
df.C = 2
df
# A B count
#0 1 2 4
#1 2 3 5
#2 3 4 6
虽然括号是向数据框添加新列的标准方法:
df['C'] = 2
df
# A B count C
#0 1 2 4 2
#1 2 3 5 2
#2 3 4 6 2
如果列已经存在,假设数据框没有同名属性(如上面的count 的情况),用点修改它是有效的:
df.B = 3
df
# A B count C
#0 1 3 4 2
#1 2 3 5 2
#2 3 3 6 2
关于python - dataframe.series 和 dataframe ['series' ] 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45156053/
有时,我倾向于(重复)执行next next a 来获取特定元素。当您需要 2 次或更少的遍历时,这很有效。然而,它很快就会变得很麻烦。对于这个简单的情况,循环的开销太大。 幸运的是,如果您知道位置,
我在使用值为 numpy 数组的 pandas.Series 时遇到了以下奇怪的行为。 % s = pd.Series([5,2], index=[6,7]) %s.loc[6] 5 <-- ret
我有一个看起来像这样的数据框(小版本): A B C 0 125 ADB [AF:12] 1 189 ACB [AF:78, AF:85, AF:98] 2 148 ADB
我在 Pandas (s1) 中创建了一个系列,用于根据原始 DataFrame 中的列 ('d1') 计算这些固定数字 (1-14) 的实例数。我想要的显示在这里(时报); s1 Last
pandas series 有两个密切相关的属性:Series.index 和 Series.index.values。 这两个中的第一个返回某些 pandas 索引类型的当前索引。它是可变的,可用于
我正在尝试使用 KNNClassifier 训练模型。我将数据拆分如下: X_train, X_test, y_train, y_test = train_test_split(X_bow, y, t
我只是尝试对我的数据框进行排序并使用了以下函数: df[df.count >= df.count.quantile(.95)] 返回错误: AttributeError: 'function' obj
我试过了 print(type(numbers[2])) numbers[2].tolist() print(type(numbers[2])) 那是行不通的。我得到了 Numbers 是一个矩阵
我想从时间戳中减去日期。settings.dataset_end_date 是一个 pandas._libs.tslibs.timestamps.Timestamp引用['date_of_patent
我有一个带有数据的 pandas.core.series.Series 0 [00115840, 00110005, 001000033, 00116000... 1 [00267285,
s = pd.Series( nr.randint( 0, 10, 5 ), index=nr.randint(0, 10, 5 ) ) s 输出 1 3 7 6 2 0 9
pandas.DataFrame.query() 方法非常适合在加载或绘图时(预/后)过滤数据。它对于方法链特别方便。 我发现自己经常想将相同的逻辑应用于 pandas.Series,例如在完成诸如返
这个问题在这里已经有了答案: Difference between map, applymap and apply methods in Pandas (11 个回答) 去年关闭。 Series.ma
我正在总结一系列中的值,但根据我如何做,我会得到不同的结果。我试过的两种方法是: sum(df['series']) df['series'].sum() 为什么它们会返回不同的值? 示例代码。 s
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
这个问题在这里已经有了答案: Pandas: select DF rows based on another DF (5 个答案) 关闭 5 年前。 如果我有一个包含开始时间和结束时间的 DataF
我尝试了 Series.index 和 Series.keys() 并且输出是相似的。我找不到它们之间的显着差异。它们是否适用于某些特殊条件? 我在 Anaconda 上的 Jupyter Noteb
我有一个(非常大的)系列,其中包含关键字(例如,每行包含多个由“-”分隔的关键字 In[5]: word_series Out[5]: 0 the-cat-is-pink 1
我需要使用 pandas.read_excel 通过 Python 获取 Excel 电子表格最后一个单元格的值。该单元格包含一个日期,我需要将其分配给 Python 脚本中的变量。格式为2018-1
我是一名优秀的程序员,十分优秀!